structure initialization

Chris Torek chris at mimsy.umd.edu
Fri Feb 2 11:58:44 AEST 1990


In article <Feb.1.09.43.53.1990.19730 at math.rutgers.edu>
bumby at math.rutgers.edu (Richard Bumby) writes:
>Is it possible that the typedef introduces the need for an extra level
>of bracketing on some compilers?

No.  Aggregate initialisers always take one level of braces for each
aggregate.  In this case---where we had

	struct exp1 {
	    int length;
	    long grp1;
	    long grp2;
	};
	
	typedef struct {
	    struct exp1 blk[5];
	} RANGE;

an object of type `RANGE' needed a brace because it was a struct,
plus a brace because the (single) element of that struct was an
array; then, each element of the array needed a brace because it
was a struct.

Had the type definition been

	typedef struct {
	    int somevalue;
	    struct exp1 b1;
	    struct exp1 b2[2];
	    int othervalue;
	} RANGE;

this might have been more obvious:

	RANGE x = {
	/* somevalue */		1,
	/* b1 */	{ 1, 2, 3 },
	/* b2 */	{
	/* b2[0] */		{ 4, 5, 6 },
	/* b2[1] */		{ 7, 8, 9 },
	/* b2 */	}
	/* othervalue */	0
	};

If you drop all but the b2/b2[0]/b2[1]/b2 lines, you get

	RANGE x = { { { 4, 5, 6 }, { 7, 8, 9 } } };
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at cs.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list