Can someone please explain why this code...
// main.c
#include <stddef.h>
static const int g_a = 1;
static const char* g_b = "hello";
static const char* g_c[] = { "a", "b", NULL };
typedef struct Foo
{
int a;
const char* b;
const char** c;
} Foo;
static Foo f[] =
{
{ g_a,
g_b,
g_c }
};
int main( int argc, char* argv[] )
{
return 0;
}
...produces this error:
> gcc --version && gcc -g main.c
gcc (GCC) 8.2.1 20181215 (Red Hat 8.2.1-6)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
main.c:19:5: error: initializer element is not constant
g_b,
^~~
main.c:19:5: note: (near initialization for 'f[0].b')
I understand that the compiler is asking for a constant to initialize f[0].b, so the following initialization is a solution to the compile error:
static Foo f[] = { { g_a, "hello", g_c } };
But why does the compiler not issue a similar "constant required" error for the initialization of f[0].c? (Or, for that matter, f[0].a?) Why is this only an issue for f[0].b?