I am not familiar at all with these techniques using char* to get rid of strings..
My questions are commented on the code below. I wanted to change the value of "str" and have this change to reflect inside the array of objects.
char str[] = "dynamic text";
String items[] = { str, "item01", "item02"}; //couldn't manage to pass reference as &str...
int counter = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
for(int i = 0; i<3; i++){
Serial.println(items[i]); // I am trying to change value of items[i] while printing.
}
Serial.println("---");
delay(1000);
counter++;
//strcpy(str, (char) counter ); //<-- actually I couldn't solve this too.
strcpy(str, "new content" );
}
reading some ideas from:
It is an excellent idea to get rid of String objects, but what you are trying to do is not clear.
Please explain, as clearly as possible. The comments make no sense.
This won't work:
counter++;
strcpy(str, (char) counter ); //<-- actually I couldn't solve this too.
First, you need to convert "counter" into a character array, then copy the character array. Most people would use the itoa() function for that.
Change String items[] = { str, "item01", "item02"};
to char *items[] = { str, "item01", "item02"};
DKWatson, thanks for that!!
jremington:
The comments make no sense.
Sorry. Originally i wanted to have an array of String objects which I could update the first object without recreating the whole array.
So I created a variable I thought I could update, ie:
String str = "dynamic text";
String items[] { str1, "item01", "item02" };
(later I converted to char[])
char str[] = "dynamic text";
char *items[] = { str, "item01", "item02"};
jremington:
First, you need to convert "counter" into a character array, then copy the character array.
what I did is super clumsy, but its working. How to improve from this or cut some lines of code?
char charCounter[2]; //up to 99.
itoa(counter, charCounter, 10);
char newStr[12] = "new content ";
char all[14] = "";
strcat(all, newStr);
strcat(all, charCounter);
strcpy(str, all);
char charCounter[2]; //up to 99.
Too short. Always add at least 1 extra byte for the terminating zero of a C-string.
"counter" is not defined.
This accomplishes the same thing, I think.
int counter=99;
char charCounter[3], str[20];
strcpy(str,"new content ");
itoa(counter, charCounter, 10);
strcat(str, charCounter);
Serial.println(str); //prints "new content 99"
Thanks! It works now.
Just to clarify this thread title. I thought an array object would store the values of passed objects.
As I add a char[] object to an array, I thought I needed, instead, to pass its reference address using '&'.
char[] text = "value01";
char *arr[] = { &text, "text1" , "text2"}
Again:I thought '&' would indicate to the compiler not to take the value ("value01"), but refer to variable 'text'. It seems arrays refers to objects by default. In this example, does compiler creates a temp char[] for "text1" and "text2" ?
Not quite. By default, when you reference a cstring without specifying an index ([]) you are in fact passing the address of index zero.
DKWatson:
Not quite. By default, when you reference a cstring without specifying an index ([]) you are in fact passing the address of index zero.
I am not sure I could understand it. "text1" and "text2" are being written on the same address of index zero?
Also, this line is giving me a deprecated conversion from string constant to 'char*' warning.
char *items[] = {str, "item01", "item02"};
I have changed it, but I have read this solution is to be avoided.
char *items[] = { str,(char*)"item01", (char*)"item02"};
finally, I am not sure if the "const" declaration before 'char' makes any difference here, or there were other options.