Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!hao!noao!mcdsun!fnf From: fnf@mcdsun.UUCP (Fred Fish) Newsgroups: comp.lang.c Subject: possible structure initialization bug Message-ID: <352@mcdsun.UUCP> Date: Tue, 11-Aug-87 16:40:42 EDT Article-I.D.: mcdsun.352 Posted: Tue Aug 11 16:40:42 1987 Date-Received: Thu, 13-Aug-87 07:17:18 EDT Organization: Motorola Microcomputer Division, Tempe, Az. Lines: 80 The following possible bug report was generated due in part to failure to compile code from a compiler test suite. I'd like to solicit opinions on whether or not the code is valid pre-ANSI C, valid ANSI C, or not valid C. Note also that Harbison and Steele go into a little more detail than the draft ANSI standard, and also introduce the notion that the "shape" of the initializers should match the "shape" of the object being initialized, which in this case I would take to mean that a valid initializer would be of the form "{{a,b},{c,d}}". -Fred /* * Possible structure initialization bug (see comments below). * Results from various compilers are: * * GNU C compiler: No complaint. * bar.foo[0][0] = 1; * bar.foo[0][1] = 2; * bar.foo[1][0] = 3; * bar.foo[1][1] = 4; * * SUN 3.2 C compiler: complains about '}' expected * (pcc based) bar.foo[0][0] = 1; * bar.foo[0][1] = 2; * bar.foo[1][0] = 0; * bar.foo[1][1] = 0; * * Motorola 68020 cc complains about '}' expected * (pcc based) bar.foo[0][0] = 1; * bar.foo[0][1] = 2; * bar.foo[1][0] = 0; * bar.foo[1][1] = 0; * * Greenhills 68020 cc complains about initializer too large * no output file generated. * * The draft ANSI C standard is quite specific about the * behavior when there are too few initializers for an * aggregate or subaggregate, but doesn't really address * the reverse situation of too many initializers (that I could * find anyway). Section 3.5.6 has this to say: * * "There shall be no more initializers in an initializer list than * there are objects to be initialized." Well, I see four objects * to initialize and four initializers, so read on... * * "... the initializer for an object that has aggregate type shall * be a brace-enclosed list of initializers for the members of the * aggregate, wirttin in increasing subscript or member order. If the * aggregate contains members that are aggregates, the rules apply * recursively to the subaggregates. If the initializer of a sub- * aggregate begins with a left brace, the succeeding initializers * initialize the members of the subaggregate. Otherwise, only * enough initializers from the list are taken to account for the * members of the first subaggregate; any remaining initializers * are left to initialize the next member of the subaggregate of * which the current aggregate is a part." * */ struct { int foo[2][2]; } bar = { {1, 2, 3, 4} }; main () { printf ("bar.foo[0][0] = %d\n", bar.foo[0][0]); printf ("bar.foo[0][1] = %d\n", bar.foo[0][1]); printf ("bar.foo[1][0] = %d\n", bar.foo[1][0]); printf ("bar.foo[1][1] = %d\n", bar.foo[1][1]); } -- = Drug tests; just say *NO*! = Fred Fish Motorola Computer Division, 3013 S 52nd St, Tempe, Az 85282 USA = seismo!noao!mcdsun!fnf (602) 438-3614