structure initialization

J. E. Apedaile jeapedai at pttesac.UUCP
Tue Jan 23 11:31:38 AEST 1990


The example code that follows compiles correctly and prints the right values
when executed on SunOS 4.0.3 and on a UNIX-PC OS 3.5 (SVR2.?).  The code that
this was taken from also compiled without error on an AT&T SVR2.2 compiler.

Gcc on the UNIX-PC gives the following warning and output:
jim 57 > gcc e.c
e.c: In function main:
e.c:21: warning: excess elements in aggregate initializer
jim 58 > a.out
40, cf000, ff000
0, 0, 0
0, 0, 0
0, 0, 0
0, 0, 0

The code fails to compile at all on an a AT&T 3B20S SVR3.1 or a AT&T 3B4000
SVR3.1.5 and gives the following error output:
cc e.c
"e.c", line 17: too many struct initializers
"e.c", line 17: too many initializers; missing } ?
"e.c", line 17: operands of = have incompatible types
"e.c", line 17: cannot recover from earlier errors: goodbye!

Lint does not complain on any of these systems.

The AT&T compiler and gcc will both compile with out errors if the five
sets of braces surrounding each group of three items in the initialization
are removed.  A quick look through K&R1 did not find anything which would 
lead me to believe that the code as written is wrong.  It seems to me that
forcing the initialization to be 1 group of 15 items instead of 5 groups of
3 items does not make code maintenance easier but harder, especially if the
structure definition and typedef are in a header file.

Why is this no longer allowed?

Any comments?

Jim Apedaile                        	     Work: (415) 545-8300
WORK UUCP: {att,bellcore,sun,ames,pyramid}!pacbell!pttesac!jeapedai
HOME UUCP: {att,bellcore,sun,ames,pyramid}!pacbell!pttesac!clab!jim


 ----------------------------- CUT HERE  -------------------------- 

struct exp1 {
    int length;
    long grp1;
    long grp2;
};

typedef struct {
    struct exp1 blk[5];
} RANGE;


main() 
{
    static RANGE example = {
	{ 40,	0xcf000,	0xff000 },
	{ 50,	0x1cf000,	0x1ff000 },
	{ 60,	0x2cf000,	0x2ff000 },
	{ 70,	0x3cf000,	0x3ff000 },
	{ 80,	0x4cf000,	0x4ff000 },
    };
    register RANGE *rptr;
    register int i;

    rptr = &example;
    for ( i = 0; i < 5; i++) 
	(void)printf("%d, %lx, %lx\n",
		    rptr->blk[i].length,
		    rptr->blk[i].grp1,
		    rptr->blk[i].grp2);
    return(0);
}

-- 
Jim Apedaile                        	     Work: (415) 545-8300
WORK UUCP: {att,bellcore,sun,ames,pyramid}!pacbell!pttesac!jeapedai
HOME UUCP: {att,bellcore,sun,ames,pyramid}!pacbell!pttesac!clab!jim



More information about the Comp.lang.c mailing list