|
From: Nicholas N. <nj...@ca...> - 2004-08-10 10:52:10
|
Hi,
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]')
And it initialises a[0] to "one" and a[1] to "four".
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.
Any ideas, or is this just impossible in C? Thanks.
[In case you're wondering: I'm looking at adding uninitialised checking
of all syscall args to Memcheck, and so I want to expand the sys_info type
to include an array of arg names for all syscalls. So the sys_info
initialisation in vg_syscalls.c would look like:
SYSB_(write, MayBlock, { "fd", "buf", "count", NULL } ),
which expands to:
[__NR_write] = { MayBlock, before_write, NULL,
{ "fd", "buf", "count", NULL} }
The arg names are necessary for producing good error messages. This could
be done relatively easily non-statically, or by breaking each syscall's
SYSB_ macro into two parts, but it would be better to keep it static and
unbroken if possible.]
N
|
|
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/
|
|
From: Nicholas N. <nj...@ca...> - 2004-08-10 12:58:38
|
On Tue, 10 Aug 2004, Tom Hughes wrote:
> 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.
Great, got it working. Thanks, Tom.
N
|
|
From: Oswald B. <os...@kd...> - 2004-08-10 11:15:39
|
On Tue, Aug 10, 2004 at 11:52:03AM +0100, Nicholas Nethercote 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 },
> };
>
just leave out the "[<idx>] =" parts and everything will be fine.
--
Hi! I'm a .signature virus! Copy me into your ~/.signature, please!
--
Chaos, panic, and disorder - my work here is done.
|
|
From: Tom H. <th...@cy...> - 2004-08-10 11:19:38
|
In message <20040810111535.GA7867@ugly.local>
Oswald Buddenhagen <os...@kd...> wrote:
> On Tue, Aug 10, 2004 at 11:52:03AM +0100, Nicholas Nethercote 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 },
>> };
>>
> just leave out the "[<idx>] =" parts and everything will be fine.
No it won't, that was the first thing I tried. You still need to fix
the inner dimension or use a compound literal.
Tom
--
Tom Hughes (th...@cy...)
Software Engineer, Cyberscience Corporation
http://www.cyberscience.com/
|