timg@jpl-devvax.JPL.NASA.GOV (Tim Graham) (08/17/88)
Hi everyone, I have a question regarding initialization of multi-dimensional arrays in C. In trying some experiments a few days ago, I found that on all the C compilers that I have access to (Sun-3 UNIX BSD 3.5, VAX Mt. Xinu UNIX 4.3), the following definition is illegal: static int test_array[][] = { { 1, 2, 3, 4, 5 }, { 1, 2 } }; With further experimenting, I determined that in an n-dimensional static array initialization, the last n-1 dimensions (the n-1 "most quickly varying" array indexes) must be specified by constant expressions. That is, the only array dimension which can be determined at compile time by the form of the initializer is the first dimension. For example, the following definition is legal: static int test_array[][5] = { { 1, 2, 3, 4, 5 }, { 1, 2 } }; Being thoroughly curious about what was going on, I went and consulted my K&R (1st edition) and H&S. In K&R, I found no mention of initializations like the first one above, but in H&S I found a reference (p. 67) which says the following: "The length of the array, a constant expression, may be omitted as long as it is not needed to allocate storage. It is not needed when: 1. The object being declared is a formal parameter of a function. 2. The declarator is accompanied by an initializer from which the length of the array can be deduced. 3. The declaration is not a defining occurrence; that is, it is an external declaration that refers to an object defined elsewhere. An exception to these cases is that the declaration of any n-dimensional array must include the sizes of the last n-1 dimensions so that the accessing algorithm can be determined." I also found a reference (p. 96) which says the following: "When multidimensional arrays are used, it is necessary to specify the bounds of all but the first dimension, so that the proper address arithmetic can be calculated. extern int matrix[][10]; /* ?-by-10 array of int */ If such bounds are not specified, the declaration is in error." Why is it that non-specification of array size declarators is illegal in the presence of complete initializers? It seems clear that there is enough information present at compile time to allocate the appropriate memory and to calculate the proper address arithmetic. The calculation does not even appear to be that difficult - a fairly simple recursion would do the trick. It would be very nice if it was possible to make declarations like the first one above. In a particular application which I have been working on, I would like to represent the types of expected parameters to various commands as integers, and store a list of integers for each command as a row in a 2-D array. That is, I would like to do something like the following: P P P P P ............. a a a a a ............. r r r r r ............. m m m m m ............. 1 2 3 4 5 ............. | | | | | ............. | | | | | ............. V V V V V ............. Command 1 ------> { { 1, 5, 7, 4, 5 }, Command 2 ------> { 2, 5, 6 }, Command 3 ------> { 1, 5, 4, 3, 5, 2, 5, 1 }, Command 4 ------> { 5, 5, 4, 4, 3, 2 } Command 5 ------> { 1 } } It would be very nice to declare this as a static 2-D array, with the initializers determining the array bounds. This way, if someone wished to add a command in the future, it would merely be necessary to add the expected parameter types as a row to this array, and array bounds and other such bothersome details would not have to be recalculated. It seems to me that since such initialization is possible for 1-D arrays it would be more consistent if it were possible for 2-D arrays. Are there any good reasons why this is not done? If not, has this issue already been brought up to the ANSI-C committee? If not, how does one go about bringing it up, and what are the chances of getting something like this incorporated for the current version of the standard? Any help that anyone can give me will be appreciated. Thank you, Tim Graham ================================================================================ "Freedom without order and order without freedom are equally destructive" - Teddy Roosevelt Jet Propulsion Laboratory/California Institute of Technology 4800 Oak Grove Drive Pasadena, CA. 91109 MS: 301-260A (818) 354-1448 UUCP: ...cit-vax!elroy!jpl-devvax!timg ARPA: elroy!timg@jpl-devvax ================================================================================