How to define a string (char array)

Hello:

PlatformIO 1.101.2 Ubuntu 24.

I am trying to setup a #define thus…

#define OUR_NAME "Well_Water_Level_0000"

then later do…

char* g_our_name = OUR_NAME;

Of course I expect this to expand to
char* g_our_name = "Well_Water_Level_0000";

But I get

warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]

at the point of the #define.

Can someone help me?

Thanks, Mark.

Simply use const char* instead of char*.

This isn’t a PlatformIO thing. This is a standard C++ thing and common practice, if not required, in C for at least the last 30 years or so.

String literals are constant data. In an environment like an ESP32 or RP2040, it will be in flash. A char* can be written to. Clearly, you can’t write to flash like that with just a store. A char* will be somewhere in RAM and a const char* will (probably) usually be somewhere in FLASH.

If you’re in a post-VAX world, strings literals will go into a page that, like the .text of your app, will be read-only and potentially shared between instances. You can’t write to things like that.

Embedded systems and “real” computers have slightly different reasons for the rules, but those have been the rules for a long time.

String literals are in constant, read-only storage.