Note: This question was influenced by this answer.
The following is valid C code:
char myString[] = "This is my string";
This will allocate a string of length 18 (including the \0
character) on the stack and assign the specified value to it.
However, the following:
char myStrings[][] = {"My 1st string", "My 2nd string", "My 3rd string"};
is not valid, giving the error "array type has incomplete element type".
So I have to specify the array like this:
char myStrings[][20] = {"My 1st string", "My 2nd string", "My 3rd string"};
Where 20 is a number which is larger than my longest string.
This compiles and works as expected.
If the compiler can dynamically sense the string length when allocating a single string on the stack, why can't it do so for an array of strings?
Edit:
Just to clarify, this is not a real life programming problem I am experiencing - this is just morbid curiosity.