Generate compiler warning if const char* array initialization comma is missing
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.
This doesn't bring the compiler in to help you, but I find writing it like below makes it easier for humans to not drop a comma:
static const char* const stateNames[STATE_AMOUNT] =
{
"Init state"
, "Run state"
, "Pause state"
, "Error state"
};
I've always used a reference to an explicitly sized array to solve this.
// no explicit size here
static const char* const stateNames[] =
{
"Init state",
"Run state",
"Pause state",
"Error state",
};
static const char* const (&stateNameVerifier)[STATE_AMOUNT] = stateNames;
http://coliru.stacked-crooked.com/a/593fc2eac80782a6
main.cpp:10:32: error: reference to type 'const char *const [5]' could not bind to an lvalue of type 'const char *const [4]'
static const char* const (&stateNameVerifier)[STATE_AMOUNT] = stateNames;
You could let the compiler count the array and generate an error message if unexpected result:
enum { STATE_AMOUNT = 4 };
static const char* const stateNames[] =
{
"Init state",
"Run state",
"Pause state" // <--- missing comma
"Error state",
};
_Static_assert( sizeof stateNames / sizeof *stateNames == STATE_AMOUNT,
"oops, missed a comma" );
See this thread for ideas to implement _Static_assert
if your compiler is very old and doesn't support it.
As a bonus, this can also help with when you add new states but forget to update the string table. But you may want to look into X Macros too.