Declaring a writable string - Help needed

Hi all

Sorry in advance, I'm a Noob when it comes to Arduino programming. From what I can see, const char* is for a read-only string.

I want to declare a string with an initial value, but I am writing code to retrieve a value from a file saved to an ESP8266., and if any value exists, to overwrite the defined value.

So I have FOO = file.readString();, but it didn't like my initial declaration of const char* FOO = "192.168.1.xx";

I hope this makes sense. Any assistance greatly received

Just remove the 'const' keyword, then you can write a new value to the string.

I really hope you didn't actually call it "FOO".

The const means constant which makes it not writable. This can help prevent errors when you really intend it to not change. It also allows the compiler to potentially do optimization when it knows the value will not change.

Can you write to this string?

char* FOO = "192.168.1.xx";

Rather than share what isn't working for me following your advice, I ask you write a small sketch that shows what you mean, as I fear I have misunderstood.

TIA

a7

Did I get it wrong? Then

// reserve space for 15 characters
char FOO[16] = "192.168.1.xx";

void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);

Serial.println(FOO);
strcpy(FOO, "192.168.111.222");
Serial.println(FOO);
}

void loop() {
}

output

192.168.1.xx
192.168.111.222

THX, order restored in the my universe. :expressionless:

The auto thing

   char FOO[] = "bob blob law";

works fine too, but wouldn't have carved out enough for a full quad string.

a7

Did you mean

char* FOO = "bob blob law";

?
Really, I think it should work... might need another coffee...

This creates a non-const pointer to a const string. Writing to it will result in undefined behaviour.

So I did the following
char* MQTT_SERVER = "192.168.1.xx";

I think hit the next error at the following point
File file = SPIFFS.open("/mqtt_server.txt", "r");
MQTT_SERVER = file.readString();

with the error
C:\Users\corma\Downloads\Sofar2mqtt\Sofar2mqtt.ino:12:21: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
12 | char* MQTT_SERVER = "192.168.1.xx";
| ^~~~~~~~~~~~~~
C:\Users\corma\Downloads\Sofar2mqtt\Sofar2mqtt.ino: In function 'void mqttReconnect()':
C:\Users\corma\Downloads\Sofar2mqtt\Sofar2mqtt.ino:586:34: error: cannot convert 'String' to 'char*' in assignment
586 | MQTT_SERVER = file.readString();
| ~~~~~~~~~~~~~~~^~
| |
| String

Yes, I tried it and posted the output. But I see your point, how about

const char* def = "192.168.1.xx";
char FOO[16];

void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);

strcpy(FOO, def);
Serial.println(FOO);
strcpy(FOO, "192.168.111.222");
Serial.println(FOO);
}

void loop() {
}

Are you following the discussion? That won't work.

Neither, will attempting to assign a String object to a char array, like

MQTT_SERVER = file.readString();

That looks fine as far as I can tell, as does your suggestion in post #5.

It is just the non-const pointer to a string literal that can become problematic.

char okay[] {"255.255.255.255"};
char* wrong {"255.255.255.255"};

okay[0] = 1;   // This is fine.
wrong[0] = 1;  // Undefined behaviour.

Thanks for the clarification.

So, with
char MQTT_SERVER[] = {"192.168.1.xx"};

I get
C:\Users\corma\Downloads\Sofar2mqtt\Sofar2mqtt.ino: In function 'void mqttReconnect()':
C:\Users\corma\Downloads\Sofar2mqtt\Sofar2mqtt.ino:586:17: error: incompatible types in assignment of 'String' to 'char [13]'
586 | MQTT_SERVER = file.readString();
| ^~~~~~~

You are repeating yourself, and I told you what you did wrong there. But, so is the compiler. It's telling you what the problem is...

The String class, and char arrays, are different types in C, and behave completely differently. You need to look up the String class function that converts a String object to a C string. It's in the reference section here.

What are you really up to? Are you merging two separate sketches that you don't understand very well?

So I tried
File file = SPIFFS.open("/mqtt_server.txt", "r");
strcpy(MQTT_SERVER,file.readString());

but got
C:\Users\corma\Downloads\Sofar2mqtt\Sofar2mqtt.ino:586:39: error: cannot convert 'String' to 'const char*'
586 | strcpy(MQTT_SERVER,file.readString());
| ~~~~~~~~~~~~~~~^~
| |
| String

Sorry if I'm misunderstanding what you are saying. How can I fix it to get it to work please ?

It seems like you're not paying attention to replies. I just told you how to fix it. Also, you did not answer my question.

Maybe a String method called
tochararray
converts a String to a char array
?

I am using the script with the WifiManager.h include. I have a custom field to get the user to add in the IP address of the MQTT Server. The "hope" was that I had the prompt initially saying 192.168.1.xx and the user then puts in the correct IP

From there, once the ESP8266 restarts with the WifiManager details stored, it will retrieve the MQTT_SERVER address saved to the file, and use it in
if(mqtt.connect(MQTT_SERVER, MQTT_USERNAME, MQTT_PASSWORD))
{
Serial.println(" connected");
delay(1000);
updateOLED("NULL", "NULL", "NULL", "CONNECTED");
delay(1000);
etc.....

There are no Arduino "scripts". But your plan sounds plausible. Good luck with your project. You're not feeding back even when I do footwork for you... at least not on the relevant issue which is the type conversion...

Apologies for wanting to be spoonfed. I have the whole thing working, except for just the string issue

Let me ask this question in another way - Am I right to be using a char or should I be looking at using String ?