Is it harmful to declare an array of pointers to string literals if it is not defined as a constant?
I'm constructing an array of strings of varied lengths. Everything appears to function OK if I set these up as an array of pointers to string literals, and I can even alter them afterward. I simply want to make sure this is the best technique before using it on a wider scale in an embedded program.
- Is there any detrimental side effect that this basic example may have overlooked?
- Is there any advantage to expressing it as a two-dimensional array of characters with a max length?
Example:
Code:
#include <stdio.h>
char * OutputNames[20] =
{
"Output 01",
"Output 02",
"Output 03",
"Output 04",
"Output 05",
"Output 06",
"Output 07",
"Output 08",
"Output 09",
"Output 10",
"Output 11",
"Output 12",
"Output 13",
"Output 14",
"Output 15",
"Output 16",
"Output 17",
"Output 18",
"Output 19",
"Output 20",
};
char main()
{
OutputNames[12] = "Test Output";
for (unsigned char ArrMem = 0; ArrMem < 20; ++ArrMem)
{
printf("%s\n", OutputNames[ArrMem]);
}
getchar();
return (0);
}
Scaler has one similar example online where they state String literals in C are read-only (despite the fact that their type is really char [].
It also states that the array length is omitted so that the size may be determined by the initializer. I'm not sure what that means literally. Could someone please explain this to me?