From: Tom H. <th...@cy...> - 2004-08-10 11:14:02
|
In message <Pin...@he...> Nicholas Nethercote <nj...@ca...> wrote: > I want to initialise a static 2-d array in C. Is this possible? > > GCC complains about this: > > const char** a[] = { > [0] = { "one", "two", "three", NULL }, > [1] = { "four", "five", "six", NULL }, > }; > > saying: > > b.c:7: warning: braces around scalar initializer > b.c:7: warning: (near initialization for `a[0]') Because a[0] is a pointer - a is an array of pointers to pointers - and you can't initialise a pointer with an aggregate. C won't just invent an unnamed array and then take a pointer to it. If the inner dimension of the array is fixed then this will work: const char* a[][4] = { [0] = { "one", "two", "three", NULL }, [1] = { "four", "five", "six", NULL } }; Because a is now an array of arrays of pointers so you can initialise the [0] member with an array. <fx: goes and reads info gcc> Actually, it seems that C99 can do it, and possibly GCC did even before that. The syntax is: const char** a[] = { [0] = (const char *[]){ "one", "two", "three", NULL }, [1] = (const char *[]){ "four", "five", "six", NULL } }; And "cc -std=c99 -pedantic -Wall" seems to compile that with no complaints as does gcc in non-pedantic c89 mode. Pedantic C89 mode refuses it. > If I break it into two pieces: > > const char* a1[] = { "one", "two", "three", NULL }; > const char* a2[] = { "four", "five", "six", NULL }; > > const char** a[] = { > [0] = a1, > [1] = a2, > }; > > it works ok, but that doesn't really suit my purpose, because there's > a macro involved and it would be much more convenient to do the former. That is the other option. Tom -- Tom Hughes (th...@cy...) Software Engineer, Cyberscience Corporation http://www.cyberscience.com/ |