Why does
static char *opcode_str[] = { "DATA"
, "DATA_REQUEST_ACK"
, "ACK_TIMER_EXPIRED"
, "ACK_UNEXPECTED_SEQ"
, "ACK_AS_REQUESTED"
} ;
work, but
static char **opcode_str = { "DATA"
, "DATA_REQUEST_ACK"
, "ACK_TIMER_EXPIRED"
, "ACK_UNEXPECTED_SEQ"
, "ACK_AS_REQUESTED"
} ;
fails with SEGV when opcode_str[0] is printf’d?
I think it’s because the second listing has not allocated memory for the five element array of pointers, but I need a more comprehensive explanation.
All the best,
Chris.
That’s correct. You’re essentially trying to assign an array to a pointer. GCC 4.4.1 warns about this by default:
It repeats the excess elements warning 4 times, since you’re essentially putting 5 pointers where only one will fit. You can use gcc -Werror to force all warnings to be errors.
You could do:
But you’ve already found the best way to do it. As far as when the error occurs, once you invoke undefined behavior you really can’t count on a particular time for problems to manifest.
But I think opcode_str holds a pointer to DATA. So (assuming 32-bit) it will try to interpret the first four bytes at opcode_str (‘D’,’A’,’T’,’A’) as as the four bytes of a char*.