Is it necessary to initialize a char array?

Hi
This works:

char filename[] = "20000101.TXT";
sprintf(filename, "%04u%02u%02u.TXT", gps.date.year(), gps.date.month(), gps.date.day());
openFile(filename);

void openFile(char* name) {
  dataFile = SD.open(name, FILE_WRITE);
}

But when I change the first line to

char filename[12];

it does not work. I think the documentation says, that I have not to initialize the array with values.
Whats going wrong here?

Whats going wrong here?

The 12 in

char filename[12];

The string "20000101.TXT" has 12 visible characters and an invisible (to you) terminating zero that denotes the end of the string

Try

char filename[13];

or better still leave out the number as in your first example and let the compiler work it out for you

wima75:
But when I change the first line to

char filename[12];

it does not work.

I suspect there is a lot of stuff you have omitted to tell us. If the array filename has no data in it I can't imagine how you think openFile(filename); could possibly work.

Please post the complete program that does not work so we can see exactly what you have tried.

I wonder if the problem is that you have not added a terminating NULL character to the text that you put into the array.

...R

UKHeliBob:
The string "20000101.TXT" has 12 visible characters and an invisible (to you) terminating zero that denotes the end of the string

Ou yeah, thank you for this fast reply.

He fills the array, so to speak, with sprintf(…), which certainly places a terminating null character.

Unfortunately after the 12 characters he's allowed as @UKHB points out.

a7\0