Teaching const
Neal Weidenhofer
nw at amdahl.uts.amdahl.com
Fri Apr 8 17:30:15 AEST 1988
In article <7788 at alice.UUCP>, bs at alice.UUCP writes:
> Try reading C declarations backwards (from right to left) they often
> make sense that way.
The way to read them is from the "inside out". Start with the identifier
being declared. The closest type/"*"/"[...]"/"(...)" describes it.
The next closest describes that and so on. Just remember that, in the
absence of parentheses, things on the right ("[...]" and "(...)") are "closer"
than things on the left ("*", etc.). The only exception is storage class
which ALWAYS applies directly to the identifier being declared.
For example:
int * const a;
decodes as:
a is a constant,
a is a constant pointer,
a is a constant pointer to an int.
(i.e., a cannot be modified but *a can.)
while:
const int * a;
means:
a is a pointer,
a is a pointer to an int,
a is a pointer to a constant int (or int constant.)
(i.e., *a cannot be modified but a can.)
and a toughie:
int (*a[])();
means:
a is an array (remember right is "closer" than left)
a is an array of pointers,
a is an array of pointers to functions,
a is an array of pointers to functions that return int.
Please, please, please, if you're teaching C, teach them to decode
declarations this way. They'll bless you for it.
> The ones that don't you might consider simplifying
> using typedef.
This is ALWAYS a good idea if declarations become unmanageable.
The opinions expressed above are mine (but I'm willing to share.)
Regards,
Neal Weidenhofer
And each one there ...{hplabs|ihnp4|ames|decwrl}!amdahl!nw
Had one thing shared... Amdahl Corporation
And wept when it was all done 1250 E. Arques Ave. (M/S 316)
For being done too soon. P. O. Box 3470
Sunnyvale, CA 94088-3470
(408)737-5007
More information about the Comp.lang.c
mailing list