Wrapping every const char*
in a pair of parenthesis should solve the problem as shown in the following snippet:
static const char* const stateNames[5] =
{
("Init state"),
("Run state"),
("Pause state") //comma missing
("Pause state3"),
("Error state")
};
If you forget a comma, you will get a compilation error similar to: error: called object is not a function or function pointer
LIVE DEMO
Note that if you forget the comma what actually happens is that C will actually concatenate the two (or more) strings until the next comma, or the end of the array. For instance let's say you forget the comma as shown in the following:
static const char* const stateNames[] =
{
"Init state",
"Run state",
"Pause state" //comma missing
"Pause state3" //comma missing
"Error state"
};
int main(void)
{
printf("%s\n", stateNames[0]);
return 0;
}
This is what gcc-9.2
generates (other compilers generate similar code):
.LC0:
.string "Init state"
.string "Run state"
.string "Pause statePause state3Error state" ; oooops look what happened
.quad .LC0
.quad .LC1
.quad .LC2
main:
push rbp
mov rbp, rsp
mov eax, OFFSET FLAT:.LC0
mov rdi, rax
call puts
mov eax, 0
pop rbp
ret
It is clear that the last three strings are concatenated and the array as not the length you would expect.