Declaration puzzler (another old question?)

David Chase chase at orc.olivetti.com
Tue Oct 4 11:16:20 AEST 1988


I can't figure out how to declare the type of an array with (say) 10
elements, each of which is that array type.  That is, I want the
non-error effect of

	typedef foo *(foo[10]);

Both the SunOS 3.4 C compiler and the GCC 1.28 compiler hate this
declaration.

The word from Harbison and Steele 2 appears to be (5.4, p. 93)

  "If T is any C type except void or 'function returning ...,'
  the array type 'array of T' may be declared."

Clearly, no matter what "foo *" is, it is not void, and it is not
a "function returning ...".

The word from Kernighan and Ritchie 2 appears to be (A8.9, p. 221)

  "typedef does not introduce new types, only synonyms for types
  that could be specified in another way."

and (A8.6.2, p 217)

  "Any type from which an array is constructed must be complete; it
  must not be an array or structure of incomplete type."

I can succeed, however, if I wrap the array up in a structure, as in

  struct mumble {
    struct mumble * children[10];
  };

or

  typedef struct mumble * glop;

  struct mumble {
    glop children[10];
  };

This is all a little puzzling to me.  If I didn't know that C was
wonderful, I might think that arrays were a second-class type.  It's
nice to know that the Good Books still need High Priests to interpret
them.  Pass me the incense and a dead chicken, please.

By the way, (in reference to a discussion on Duff's device), both
compilers mentioned above to fairly reasonable things with the
following apparently legal (based on a quick scan of H&S 2) trick:

  typedef int bogus[1000];
  typedef struct {
    bogus x;
  } * bogus_wrapper;
  bogus a,b;

  ...

  * (bogus_wrapper) a = * (bogus_wrapper) b;

(Sun spit out a very tight loop, Gcc called bcopy.)

Oh well.  Back to work.  I don't know why I ask questions like this,
since I already know the answer.

David
(chase at orc.olivetti.com, or oliveb!orc!chase, but NOT oliveb!chase)



More information about the Comp.lang.c mailing list