[comp.lang.c] initializing an array

reeder@ut-emx.UUCP (William P. Reeder) (05/05/89)

I can initialize a simple array like this:
	int a[] = { 1, 2, 3 };
and C will figure out that a must have 3 elements.  But what if
my array has two dimensions?
	int a[][] = {
		{ 1, 2, 3 },
		{ 4, 5, 6 },
		{ 7, 8, 9 },
	};

It is possible to determine from the syntax how many elements there
are in each dimension, but my compiler complains about a null
dimension.

Section 8.6 of K&R (1st edition) says:

	When the declared variable is an aggregate (a
	structure or array) then the initializer consists
	of a brace-enclosed, comma-separated list of
	initializers for the members of the aggregate,
	written in increasing subscript or member order.
	If the aggregate contains subaggregates, this
	rule applies recursively to the members of the
	aggregate.

The last sentence claims that the initialization process is recursive.
Well, if determining the size of an (one dimensional) array can be done
during initialization, why not for the sub-array (a 2-d array is really
just an array of arrays, right?) ?

Any thoughts?  Please reply via e-mail to reeder@emx.utexas.edu
(or uunet!cs.utexas.edu!ut-emx!reeder) since I don't normally read
this group.

William Reeder, University of Texas at Austin, Computation Center
reeder@emx.utexas.edu
-- 
DISCLAIMER:	I speak only for myself, and usually only to myself.

matthew@sunpix.UUCP ( Sun Visualization Products) (05/09/89)

In article <12814@ut-emx.UUCP>, reeder@ut-emx.UUCP (William P. Reeder) writes:
| I can initialize a simple array like this:
| 	int a[] = { 1, 2, 3 };
| and C will figure out that a must have 3 elements.  But what if
| my array has two dimensions?
| 	int a[][] = {
| 		{ 1, 2, 3 },
| 		{ 4, 5, 6 },
| 		{ 7, 8, 9 },  <------ remove this excess ','
| 	};
| 
| It is possible to determine from the syntax how many elements there
| are in each dimension, but my compiler complains about a null
| dimension.
| 

Of course it going to complain about a null dimension.  You've got an extra ','
in the initializer declaration.


-- 
Matthew Lee Stier                         |
Sun Microsystems ---  RTP, NC  27709-3447 |        "Wisconsin   Escapee"
uucp: { sun, mcnc!rti }!sunpix!matthew    |
phone: (919) 469-8300 fax: (919) 460-8355 |

bill@twwells.uucp (T. William Wells) (05/11/89)

In article <609@greens.UUCP> matthew@sunpix.UUCP ( Sun Visualization Products) writes:
: |     int a[][] = {
: |             { 1, 2, 3 },
: |             { 4, 5, 6 },
: |             { 7, 8, 9 },  <------ remove this excess ','
: |     };
: |
: | It is possible to determine from the syntax how many elements there
: | are in each dimension, but my compiler complains about a null
: | dimension.
:
: Of course it going to complain about a null dimension.  You've got an extra ','
: in the initializer declaration.

Wrong. A trailing comma in an initializer is perfectly legal. The
problem with his code is that, while what he wants is possible, it is
not part of the language.

---
Bill                            { uunet | novavax } !twwells!bill