Is typedef char BUFFER[20] legal?
Avery Colter
avery at netcom.COM
Fri Feb 8 06:05:10 AEST 1991
One of the high masters has given me a sound rapping on my shoulder
about typedefs.
So I went back to read the books I have, and guess what, they didn't
detail the typedeffing of arrays.
In fact, all they detailed were typedefs of pointer to simple objects
and of structures.
So, these two books left me with the mistaken notion that typedef
was some kind of macro with two arguments, the second being the
name of the derived type and the first being like a typecast.
Make not this mistake, fellow students,
lest the master give YOU a sound rapping.
So, for those who also might not have books
which were very clear about typedefs....
My impression now, preparing my shoulder for more, is that
typedef is tacked in front of a normal declaration in order
to have the name treated as the name of a type rather than
the name of an object, and that declaring a variable of the
derived type is equivalent to making a non-typedef declaration
exactly like that to which typedef was prepended, only with
the new variable name occurring exactly where the derived
type name was in the typedef declaration.
In other words:
type embellished-name
says "name is an object, and the application of the given embellishements
to this object yields a value of the leading type".
Note: put "tag" in place of "object" when the name refers to an
array or other composite that is not strictly an object.
So, float (*thing)(int); says, "thing is an OBJECT whose indirection
is a function of one int parameter whose result is a float".
A pointer to a float function of int.
float (*thing[23])(int); says, "thing is the TAG of a 23-member array
of objects whose indirections are tags of functions with int parameters
which return float values." Or, "23-member array of pointers to
float functions of int".
(Array names are not strictly objects, right? Like a pointer, a direct
reference to it yields an address value, but unlike a pointer the
array name's value itself cannot be changed.)
typedef type embellished-name
says "name is a type, objects of which yield values of the base type
when subjected to the prescribed embellishments".
And therefore, typedef float (*thing[23])(int); says,
"thing is NOT an object or a TAG, but a TYPE, and tags of this TYPE are
23-member arrays of pointers to float functions of int".
--
Avery Ray Colter {apple|claris}!netcom!avery {decwrl|mips|sgi}!btr!elfcat
(415) 839-4567 "I feel love has got to come on and I want it:
Something big and lovely!" - The B-52s, "Channel Z"
More information about the Comp.lang.c
mailing list