[net.lang.c] array and structure initialization

knutsen@sri-unix.UUCP (06/10/83)

Say I have a structure foo

struct foo {
	char *f_slist[];
	int f_code;
}

	and I want to initialize one. Since

char *foo[] = {"aaa", "bbb", "ccc", ""};

	works, I tried

struct foo foobar = {{"xx", "yy", ""}, 1};

	and got "0-length row: f_slist" and "too many initializers: f_slist"
errors. Any ideas?

	Thanks...
Andrew Knutsen SRI

mat@hou5e.UUCP (06/12/83)

Andrew:
	regarding the 

struct foo
{
	char	*f_slist[];
	int	f_code;
};

This sort of cruft went away with v6.  Remember that structures can be
assigned nowadays.  This requires that the structure length be stable
for a given type of structure.

Unfortuneately, it gets a little messy to do what you want, but this should
do it

char	*f_list[]
={
	"aaa",
	"bbb",
	"ccc",
};

struct	foo
{
	char	**f_list;
	int	f_code;
}	bar[]
={
	{ f_list, 1 },
	{   . . .   },

	  .  .  .
};
If you have access to the v7 or later source for the Ritchie C compiler
you may want to look at the output of the table encoding program.
I can't say any more about that, but you have got a nuisance problem --
there is no general way to write constants for arbitrar compound objects
except in a few (admittedly VERY useful) cases in initializers.  ADA (TM
of DOD and all that rot) does solve this problem, but the fairly general
solution is probably part of what gives the language it's horrid complexity.

						Mark Terribile

steve@whuxk.UUCP (06/13/83)

In reference to why the statements
   struct foo {
	char *f_slist[];
	int  f_code;
   }

   struct foo foobar = { {"xx", "yy", ""}, 1};

leads to compiler errors, the problem lies with the definition of the foo
structure.  The member f_slist cannot be declared as an array of character 
pointers in that fashion--it must be given a size.  The only places that
such a declaration is allowed (i.e. an array with no explicit dimensions)
is 
	1. When declaring an array with a list of initializers
           (e.g.   char *ptrs[] = { "abc", "def", "ghi" };
	2. When declaring an externally-referenced array.
 	3. When declaring a formal parameter array.

If you try adding a dimension to f_slist, then you will find that your 
initialization will work correctly.

						Steve Kochan
						BTL Whippany