C type declarations
Morris M. Keesan
keesan at bbncca.ARPA
Sat Jan 19 02:14:34 AEST 1985
-------------------------------
Indeed, changing the syntax of C type declarations at this point is totally
infeasible and would break lots of existing programs. (The previous article
suggested parsing type declarations from left to right instead of by
precedence.) However, there is another way to greatly simplify C type
declarations that was added to the language several years ago, in V7 UNIX(tm).
This is the typedef facility, and allows you to break up your declaration into
smaller pieces. E.g., instead of
int i; /* integer */
int if(); /* integer function */
int *ip; /* Integer Pointer */
int *fpi(); /* Function returning Pointer to Int */
int (*pfi)(); /* Pointer to Function returning Int */
int (*(apfi[15]))(); /* Array of 15 Pointers to Functions returning Int */
and other declarations of increasing complexity, you can say:
int i;
typedef int Intfunc();
typedef int *Intptr; /* So far, about the same */
typedef Intptr FuncretIntptr(); /* Here is where it starts to get simpler */
typedef Intfunc *PtrtoIntfunc;
PtrtoIntfunc apfi[15]; /* Isn't this one much more readable than the above? */
and of course you can choose the names for these types to conform to your own
mnemonic ideals. These days, I never write a declaration with more than one
sort of type symbol in it (from the set *, (), and []), and I find my
declarations are much more readable and consistently bug-free.
--
Morris M. Keesan
{decvax,linus,ihnp4,wivax,wjh12,ima}!bbncca!keesan
keesan @ BBN-UNIX.ARPA
More information about the Comp.lang.c
mailing list