Ethereal-dev: Re: [ethereal-dev] Complex initializers

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: Guy Harris <guy@xxxxxxxxxx>
Date: Fri, 10 Dec 1999 16:57:47 -0800 (PST)
> I am seeking to do the following:
> 
> struct something {
> 
>   char *n1;
>   char *n2;
>   char *list[];
> };
> 
> struct something st[] = {
>   { "a1", "a2", {"a3", "a4", "a5", NULL}};
> 
> with lots of initializers like the above, where the list element can have
> any number of things in it.

Unfortunately, "char *list[]" declares an array of pointers to
"char", which would, given that there's no size between the "[]"s, make
"struct something" a variable-length structure, with the length
dependent on the size of the "list" array.

C doesn't support variable-length arrays; you can have a fixed-length
array whose size is specified by the number of initializers it has, or
can externally declare an array whose size is fixed but unknown, but
those aren't true variable-length arrays.

In addition, all members of an array must be the same size, so, even if
C *did* allow you to have variable-length arrays like that, making
"struct something" a variable-length structure, you couldn't have an
array of them.

To make an array of structures like that, you'd have to pick a fixed
maximum size for "list".

Alternatively, you could declare it as

	struct something {
		char	*n1;
		char	*n2;
		char	**list;
	}

and declare the lists as arrays of their own, and initialize the "list"
members of the elements of the "struct something" to point to those
arrays.