double (*parray[15])[];

Topher Eliot topher at cyb-eng.UUCP
Fri May 23 02:18:28 AEST 1986


(I have dropped net.unix from the newsgroups list).
There has been some debate on what the declaration
	double (*parray[15])[];
means.  David Herron seems to be arguing that it declares a zero-length
array of some sort, which some people and/or compilers might interpret as
being a pointer of the same sort.  I admit I found his note a little hard
to follow, so maybe I misunderstood him.  But I thought I would add my bit
of net volume in the form of an explanation of HOW I would go about parsing
such a statement.  My basic rule, which I have trusted for many years, is
to parse it as if it were an expression, starting inside the parentheses,
and obeying operator precedence rules:

	double (*parray[15])[];
		       ^^^^
1.  It's an array with 15 elements

	double (*parray[15])[];
		^^^^^^^^^^^
2.  It's an array of 15 pointers.

	double (*parray[15])[];
	       ^^^^^^^^^^^^^^^
3.  It's an array of 15 pointers to arrays (the sizes of these arrays being
pointed at is not specified).

	double (*parray[15])[];
	^^^^^^^^^^^^^^^^^^^^^^
4.  It's an array of 15 pointers to arrays of doubles.

There's another way to parse it, sort of from the outside in:

	double (*parray[15])[];
               ^^^^^^^^^^^^^^^
1.  The expression (*parray[15])[] has type double.

	double (*parray[15])[];
               ^^^^^^^^^^^^^
2.  The expression (*parray[15]) has type "array of double", with the size
of the array unspecified.

	double (*parray[15])[];
                 ^^^^^^^^^^
3.  The expression parray[15] has type "pointer to array of double".

	double (*parray[15])[];
                 ^^^^^^
4.  parray has type "array of pointers to arrays of doubles", parray has 15
elements.

Notice that both these methods are nice and methodical, and they both come
up with the same result.

I get the impression that at least one of the participants of this
discussion parsed the declaration something along the lines of:

	double (*parray[15])[];
	                    ^^
1.  It's a zero-length array of somethings.

	double (*parray[15])[];
		^           ^^
2.  It's a zero-length array of pointers to something.

	double (*parray[15])[];
		^^^^^^^^^^^^^^
3.  It's a zero-length array of pointers to 15-element arrays of
something.

	double (*parray[15])[];
	^^^^^^^^^^^^^^^^^^^^^^
4.  It's a zero-length array of pointers to 15-element arrays of doubles.

It is my contention that parsing a declaration this way is just plain
wrong, but I admit it's an easy mistake to make, one that I myself made
back before I had given this subject as much thought as I have.

The original discussion involved lint and malloc; I have nothing to add to
that aspect of it, so I am not repeating it here.

Cheers,
Topher Eliot           Cyb Systems, Austin, TX          (512) 835-2266
 {gatech,harvard,ihnp4,nbires,seismo,ucb-vax}!ut-sally!cyb-eng!topher



More information about the Comp.lang.c mailing list