Considering the following code snippet:
enum test {
A,
B,
C
};
static const char *const table[] = {
[A] = "A",
//[B] = "B",
[C] = "C",
}; // the string representation is not (always) equivalent to the enum identifier
If I accidentally miss an entry (B
in this case), I'd like to get a compiler warning or error.
Tried with clang -Weverything
and multiple gcc
warnings (but no warning - compiles silently).
Also sizeof table / sizeof *table
is still 3
.
Or is there a way in C to check at compile time that all array elements are non-NULL?
// C++ variant
constexpr bool is_array_nonnull(const char *const array[]) {
for (int i = 0; i <= sizeof array / sizeof *array; ++i)
if (array[i] == nullptr)
return false;
return true;
}
static_assert(is_array_nonnull(table));
edit: make requirements and tested steps more clear