Array of string not working

Hello all,
I’ve already had an experienced in arduino. I’m using the arduino with the Time.h library that every time that the infra red sensor HCSR501 detects a presence of something, arduino records in the SD card the time. My code for this one is working fine. Now I started to use the ethernet shield and in order to see a log of last 10 detections in the web page created in arduino.
The example of code that I am using to create the array of sring is below:
char* myStrings={“0”,“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”}; //
strcpy(str,“Detected, “);
strcat(str,", “);
if (hour()<10){strcat(str,“0”);}
if (minute()<10){strcat(str,“0”);}
if (second()<10){strcat(str,“0”);}
the variable str is that: “Detected, 1, 22:00:01”, and it is being created correctely.

k=k+1; if (k>9){k=0};

I start with k=0, and when I set myStrings[0]=str; position [0] is set correctly, and I increment k to 1. But when I set the new str variable to myStrings[1], both myStrings[0] and myStrings[1] are being set to the new str variable. Each time that I increment the k value all the k positions are set too myStrings[0]=myStrings[1]…=myStrings[k]=str.

I’ve already seached in the forums of C++ or arduino forum and I did not find any topic about that.
Anyone can help me do solve this problem?

Please use code tags for code (the # button// like this

Please post the entire sketch - I cannot see the actual declaration of str for instance,
so cannot really comment on what the code might be doing. You cannot legally
copy over a string constant BTW, so str should be a char array large enough for
all possible contents you may catenate into it.

It's hard to guess what you're trying to do without seeing the whole code, but your use of the myStrings array looks pointless to me. The code writing the string to str could also be condensed to a single call to snprintf().

// assumes str is declared as a char array big enough to hold the resulting string plus null terminator
snprintf(str, sizeof(str), "Detected, %d, %02d:%02d:%02d", dias, hour(), minute(), second());

I suggest you post a complete sketch that demonstrates the problem, explain what you want the code to do and what it actually does, and perhaps somebody will suggest how to make it do what you want.

myStrings is an array of pointers. If you make all the pointers point to the same string, is it unreasonable that that all print the same string later?

Making a pointer point to a memory location does NOT cause any data to be copied. I suspect that that is what you are expecting to have happen. You could use

myString[n] = strdup(str);

to duplicate the string in str, and make myString[n] point to the duplicate. Keep in mind that you need to free that copy at some point, or you will leak memory.

As I sad before the string str that is declared in the code is working correctly. Because that I did not put all code.

Thanks MarkT, this is the first time that I wrote a post.

Thanks PeteH to help me to condensate the code of str.

Thanks PaulS, doing what you explained solve the problem.