Printing character array

I’'m busy with an array of strings.
I want to get the filenames of my SD card in an array.
Then I want to print the array of filenames on an LCD screen (6 rows)
Now I’m having a strange problem.
In the function I’ve made the result prints well to the serial port and in the main loop it doesn’t. I get strange characters

What’s wrong?

array_of_string_vb3stephan.ino (720 Bytes)

This is the code:

char* myFiles[16]; // number of filenames is max. 16

String fileone = “DATA.TXT”;
String filetwo = “FILE2.TXT”;
String filethree = “DATA333.TXT”;

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

void toChArray(String str, int arrayIndex) {
char buffer[16];
int len = str.length();
for (int i = 0; i < len; i++){
buffer = str.charAt(i);

  • buffer[i+1] = ‘\0’;*
  • }*
  • myFiles[arrayIndex] = buffer;*
  • Serial.print("myFiles in function: ");*
  • Serial.println(myFiles[arrayIndex]);*
    }
    void loop(){
  • toChArray(fileone, 0);*
  • toChArray(filetwo, 1);*
  • toChArray(filethree, 2);*
  • for (int i = 0; i < 3; i++){*
  • Serial.print("myFiles in main loop: ");*
    _ Serial.println(myFiles*);_
    _
    delay(500);_
    _
    }_
    _
    }*_

In the toChArray() function you are replicating the behavior of the String::toCharArray() function. Why?

  myFiles[arrayIndex] = buffer;

buffer is local to the function. It goes out of scope when the function ends. Then, myFiles[n] points to reused space. That, obviously won't work.

You need to copy the data in buffer to somewhere that will persist when the toChArray() function ends.

myFiles[arrayIndex] = strdup(buffer);

Be careful with memory allocation functions, like strdup(), since memory is very limited on the Arduino.

Your solution works! Thanks.

I could not solve it with toChrArray.
Strange output!

See code below:

char* myFiles[16] ; // aantal filenamen is 16

String fileone = “DATAFILE.TXT”;
String filetwo = “DATAFIL2.TXT”;
String filethree = “FILE3333.TXT”;

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

}

void loop(){
fileone.toCharArray(myFiles[0], 16);
filetwo.toCharArray(myFiles[1], 16);
filethree.toCharArray(myFiles[2], 16);

for (int i = 0; i < 4; i++){

Serial.println(myFiles*);*

  • delay(500);*
  • }*
    }

Your solution works! Thanks.

Not that way it doesn't!

myFiles[0] is a pointer. Where does it point to?

  fileone.toCharArray(myFiles[0], 16);

You just asked this function to put up to 16 characters in the space pointed to by myFiles[0]. Where did it put them?

What you can do is this: char buffer[16]; // Some real space fileone.toCharArray(buffer, 16); // Copy the data to the real space myFiles[0] = strdup(buffer); // Now copy the data

buffer can now go out of scope, but myFiles[0] will still point to the copy. Be sure to free(myFiles[0]); before myFiles goes out of scope/is overwritten.

Ok, now toCharArray works too!.

But the real mechanism is not 100 % clear to me.

I can't find an article that explains this stuff a littlebit easy.

I can learn C++ books but that's far away from my knowledge.

Thank you for your help.

Derk