Multidimensional Static Array Initialization Follow-up

Richard A. O'Keefe ok at quintus.uucp
Fri Aug 26 17:06:11 AEST 1988


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



More information about the Comp.lang.c mailing list