Ike Naar said:
:> Is code like the following allowed? I am talking about the comma
:> after the last function in the initializer.
:>
:> void f(void) {puts("f");}
:> void g(void) {puts("g");}
:>
:> struct Funcs { void (*f[])(void); };
:>
:> struct Funcs funcs =
I didn't notice late last night, but this obviously needs to be
struct Funcs funcs[] =
No, it doesn't. Look closely at the typedef for 'Funcs'.
I don't follow C99 weird-array syntax closely enough to tell if
that typedef is legal, but GNU C accepts it with a warning. (This
means it's probably not standard C. Add a '2' inside the '[]' in
the typedef if you want to compile it.)
*This*, however, definitely needs to be
{ {f,g,} };
(two pairs of braces). One pair for the struct and an inner pair for
the initializer for the array member.
Yes, this is sometimes the appropriate thing to do, if the processing
code is written that way, and if the wasted storage is insignificant.
Just in case you didn't see it (I can't tell from your response),
I think Ike's point was that if your code-generator dumps a bunch
of
xxx,
followed by the string
NULL
}
then you don't have to worry about the extra trailing comma. The
NULL itself can be ignored --- /if the processing code is written
that way/, of course. ;-)
Sometimes you don't have control over it; a lot of my code to generate
lists is for use in other ways (CSV files, SQL queries, etc.), which
do not allow for excess commas or 'NULL.' It's best to learn good ways
to generate such lists without depending on historical C compiler
allowances. The overhead is so minimal that it can almost always be
ignored.
Modulo Dan's comments about "historical" (hey, 1989 sounds pretty
historical to *me*!
, you're right.
-Arthur