Change string

Hello,

I have defined the following:

char MotorLeftState[4] = "FWD";

Now I would like to change this to during runtime:

char MotorLeftState[4] = "REV";

but this gives the following error:

error: invalid array assignment At global scope: :-/

I've also tried:

char MotorLeftState[] = "FWD";
char MotorLeftState[] = "REV";

but this generates the same error?

How can I redefine a char arry (string) during runtime?

Thanks in advance,

/me

You can't redeclare it, but you can use the standard C library function strcpy() to put something else in the array.

If you initialize the array the way that you showed, you must make absolutely certain that you don't try to copy a longer "string" to that array. (See Footnote.)

void setup()
{
    Serial.begin(9600);
}
void loop()
{
    char MotorLeftState[4] = "FWD";//Three chars plus terminating zero byte
    Serial.print("MotorLeftState = ");
    Serial.println(MotorLeftState);

    strcpy(MotorLeftState, "REV");
    Serial.print("MotorLeftState = ");
    Serial.println(MotorLeftState);
    Serial.println();

    delay(5000);
}

Regards,

Dave

Footnote:
It is the programmer's responsibility to make absolutely sure that the new "string" is no longer than the one that was used to initialize the array.

Instead of using a string literal to initialize, you can make the array any size you want and then strcpy() whatever you want:

    // Thsee messages can't have more than 14 characters plus terminating zero byte
    char msg[15];
.
.
.
    strcpy(msg, "Hi, Everyone.");//13 plus terminating zero byte
    Serial.print("First message : ");
    Serial.println(msg);
    
    strcpy(msg, "Bye, now!");//9 plus terminating zero byte.
    Serial.print("Second message: ");
    Serial.println(msg);
    Serial.println();

Thanks for the explanation :slight_smile:

The string I use will never exceed 3 characters, so I don't have to worry that it will become too long.

/me

Or simply:

char* string;

void setup ()
{
  string ="THIS";
}

void loop ()
{
  string ="THAT";
}

You need to cast the constant strings to (char*), otherwise the compiler will complain about a type mismatch:

char* string;

void setup() {
    Serial.begin(9600);
}


void loop() {
  string = (char*)"THIS";
  Serial.println(string);
  delay(1000);
  
  string = (char*)"THAT";
  Serial.println(string);
  delay(1000);
}

You need to cast the constant strings to (char*), otherwise the compiler will complain about a type mismatch

I'm at 0017 - no complaints there.

@ mromani

You need to cast the constant strings to (char*)

When compiling from within the Arduino Environment (arduino-0018), compiler warnings are turned off, so many (most?) readers on this forum will never see the warning.

For people compiling (by whatever means) with warnings turned on (a Good Thing, in my opinion), the proper fix (in my opinion) is to use const in the declaration, not to throw away the warning against "const correctness."

Those warnings are clues that may give some help in avoiding certain problems, but only if you heed and correct the conditions that gave them (instead of putting some kludge that merely suppresses the warnings).

const char *str;

void loop()
{
    str = "THIS";
.
.
.

No warnings. If your program tries to change something in *str, the compiler properly gives an error (even with warnings turned off).

Regards,

Dave

"Fix it; don't fake it."
---davekw7x

For people compiling (by whatever means) with warnings turned on...

Have a look here:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1278712207/11