Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!mcnc!rti-sel!dg_rtp!throopw From: throopw@dg_rtp.UUCP (Wayne Throop) Newsgroups: comp.lang.c Subject: Re: Initialization of a two-dimen. array of structures. Message-ID: <1457@dg_rtp.UUCP> Date: Thu, 19-Mar-87 17:30:50 EST Article-I.D.: dg_rtp.1457 Posted: Thu Mar 19 17:30:50 1987 Date-Received: Sat, 21-Mar-87 06:47:36 EST References: <2173@ncoast.UUCP> Lines: 64 > simpsong@ncoast.UUCP (Gregory R. Simpson @ The North Coast) > I have a structure like this: > struct of_stuff { > char *name; float value; float another; int counter; > } > stuff[20][30] = {{ [...value-list...] },{ [...value-list...] }}; > This works fine on VMS 4.5 (VMS C - 4.?) and on my PC using MS C 3.0. > However, when I tried it on a vax 11/780 running 4.3BSD and a Sun workstation > running whatever they call Unix (the latest 3.0 I believe), I got an error. > The error was repeated 4 times for each line that has a name in it > (like "joe"), the error said I was missing a }. (or something to that effect.) Right. You are missing a level of curly braces. Some compilers are forgiving of this, and some are not. It is probably best to put in all levels of braces necessary, even though I think K&R and X3J11 allow some shortcuts. This example linted and compiled correctly for me: typedef struct stuff_s { char *name; float value; float another; int counter; } stuff_t; stuff_t stuff[20][30] = { { { "00", 1, 2, 3 }, { "01", 4, 5, 6 } },{ { "10", 7, 8, 9 }, { "11", 10, 11, 12 } } }; > My question is: What is the proper way to initialize a two dimensional > array of structures... Well, the idea I keep in mind is that what follows the equal sign is a SINGLE VALUE that is the initial value of the object you are declaring. If that object is an aggregate object (an array or a structure), it becomes a curly-brace-enclosed comma-separated list of member initial values. From there we recurse. So, since what we have here is an array of array of struct of four members, we open a curly (we now are ready to give a value for an array of struct of four members) open another curly (we now are ready to give a value for a struct of four members) open another curly (we are now ready to give a value for a struct member) list four values separated by commas, close a curly, insert a comma (we are now back up one level, giving the next value for an array of struct of four members), open a curly.... and so on and on. Note that in the example I've given, I've initialized stuff[0][0], stuff[0][1], stuff[1][0], and stuff[1][1]. The problem is, in the original example, it wasn't totally clear just what was being initialized to what. The extra level of curly braces makes it unambiguous. -- Big Mac had a heart attack in the back of a yellow cab, After he'd stiffed his waitress and run out on his tab. By the time the sound of the siren said the ambulance was coming, His heart had stopped beating, but the meter was still running. Life is hard. --- Timbuk3 -- Wayne Throop !mcnc!rti-sel!dg_rtp!throopw