Path: utzoo!utgpu!attcan!uunet!lll-winken!lll-tis!ames!zodiac!joyce!sri-unix!quintus!ok From: ok@quintus.uucp (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: Multidimensional Static Array Initialization Follow-up Message-ID: <317@quintus.UUCP> Date: 26 Aug 88 07:06:11 GMT References: <2682@jpl-devvax.JPL.NASA.GOV> <13060@mimsy.UUCP> <8584@ihlpb.ATT.COM> <8596@ihlpb.ATT.COM> <13219@mimsy.UUCP> Sender: news@quintus.UUCP Reply-To: ok@quintus.UUCP (Richard A. O'Keefe) Organization: Quintus Computer Systems, Inc. Lines: 31 Various people have been arguing back and forth about declarations like int a[][] = {{1}, {2,3,4}, {5,6}}; suggesting that the compiler should figure out the bounds or giving reasons why it doesn't. I'd just like to point out that there are lots of things I'd like in an initialisation sublanguage which aren't there (in particular, calls to functions like sin() are not allowed, oh the pain), but that that restriction needn't get in the way. There is no law that says every character in the input to the C compiler had to be written by hand. It's quite straightforward to put together a little mini-language (using the C pre-processor) where you can write e.g. DEF_TWO_D("int", "a") ROW IVAL(1) END_ROW ROW IVAL(2) IVAL(3) IVAL(4) END_ROW ROW IVAL(5) IVAL(6) END_ROW END_TWO_D and compile it with a C compiler, then run it to generate a data.c and data.h file. Hint: the heading turns into something like { static int INI_rmax = 0, INI_rcur = 0; static int INI_cmax = 0, INI_ccur = 0; if (pass == 2) { fprintf(C_file, "%s %s[%d][%d] = {\n", "int", "a", INI_rmax, INI_cmax); fprintf(H_file, "extern %s %s[%d][%d];\n", "int", "a", INI_rmax, INI_cmax); } Using a mini-language like this lets you compute arbitrary numeric initial values, and makes absolutely sure that your .c and .h file are in agreement. You can even do things like ROW for (i = 0; i < N; i++) IVAL(f(i)) END_ROW