Load new string into char array?

OK, I'm going nuts trying to figure out how to load a new set of text into a char register. I have my register initialized like this:

char textString[32];

Later in the program, individual character are pulled from the array using getChar. This works just fine if I preload text into the array like this:

char textString[32] = "SAMPLE TEXT";

But, what I need to do is randomly choose a phrase using random() and switch case and load it into textString, then do the various operations on it. I've tried to get it working using strncpy, strcpy, memcpy, etc... But I just can't get it to do anything. If the code actually compiles, it causes the Arduino to hang when it gets to that line. I need to have dozens of strings to choose from so I can't just declare all of them as their own arrays!

So what's the magic formula here? Thanks for your help!

How about storing the strings in progmem?

They sort of will be. I’ll basically have a switch case set up, and at some point the program will pick a random number, go to a case, and load the string from that case into the array.

But the question is, how do I copy the chars from the string into the array?

I tried this but it didn’t work:

char newText[32] = "NEW TEXT"; //this char is defined inside loop

for(int i=0, i<32, i++){
    textString[i] = newText[i];
}

strcpy

Great!! Now how do I use it?

OK, I got it working.

requestString is a global variable declared as such:

char requestString[32];

later in the code, the contents of the array can be changed with:

strncpy(requestString, "NEW", 32);

I used strncpy because it fills the rest of the array with null characters.

I used strncpy because it fills the rest of the array with null characters.

But, since string processing ends with the first NULL encountered, you only need one. Having the array already full of NULLS allows you to forget about needing to NULL terminate an array that is a string, which is a poor programming practice to develop.

Much better is to use strcat or strcpy which adds just one NULL, and then learn to add a NULL after each character explicitly added to the array.

Just my 2 cents worth.

That's good to know. I'm definitely new to programming in general, and this particular project is my first on the Arduino. So, I'll remember your advice - I want to learn good practices right off the bat!

@jeff-0

char requestString[32];

later in the code, the contents of the array can be changed with:

strncpy(requestString, "NEW", 32);

The reason to use strncpy instead of strcpy is to prevent overflowing the target array (and not as a lazy-man's way to superflously put zeros in unused positions). If there are too many chars in the source, the strncpy doesn't put the trailing zero byte in the target array. When, later, you use that array for any of the C-style string functions, the result will be undefined behavior, since you didn't terminate the "string" in the array with a zero byte.

The proper way to use strncpy would be something like this:

char requestString[33];

later in the code, the contents of the array can be changed with:

strncpy(requestString, whatever, 32);

That way the trailing zero byte is guaranteed.

Some people might say that good programming style might be to use sizeof(requestStirng)-1 as the length argument in strncpy rather than hard-code it with a number that is one less than the array size. That way, if you decide to change the size later, you won't have to go through your code trying to find all of the places where you used it.

Of course if you absolutely, positively, unequivocally know that the string is not too long to hold in the target you could us strcpy.

However...

That's a time bomb that may lead to undefined behavior if you "forget" how big the array is and (some day) try to copy a string that's too large.

Regards,

Dave