There's also another method in C99, especially if you want named indexes, allowing for instance localization and such.
enum STRINGS {
STR_THING1,
STR_THING2,
STR_THING3,
STR_THING4,
STR_WHATEVER,
STR_MAX /* Always put this one at the end, as the counting starts at 0 */
/* this one will be defined as the number of elements */
}
static const char *foo[STR_MAX] = {
[STR_THING1] = "123",
[STR_THING2] = "456",
[STR_THING3] = "789",
[STR_THING4] = "987",
[STR_WHATEVER] = "OR Something else",
};
By using named initializer the program still is correct even if an enum value changes.
for (i = STR_THING1; i<STR_MAX; i++)
puts(foo[i]);
or anywhere in the program with the named index
printf("thing2 is %s\n", foo[STR_THING3]);
This technique can be used to simulate ressource bundles. Declare one enum
and several string arrays with language variants and use a pointer in the rest of the program. Simple and fast (especially on 64bit machines where getting the address of a constant (string) can be relatively costly.
Edit: the sizeof foo/sizeof *foo technique still works with this.