[net.lang.c] C type declarations

jrv@Mitre-Bedford (01/13/85)

I (think I) understand the C types involving the operators *,
(), and [], but I find the C syntax confusing. Judging from
recent discussions on multidimensional arrays and functions
returning pointers, I'm not alone.  I'd like to bring up a
suggestion I ran across several years ago: to replace the
prefix operator * with the postfix operator ^.  The three
operators ^, (), and [] can have the same precedence, and are
executed from left to right. The declaration syntax is also
changed to put the basic type last, so that a declaration can
be read from left to right...
        var a()^[]^ : int;
declares a to be a FUNCTION returning a POINTER to an ARRAY
of POINTERS to INTEGERS. The colon can move without changing
the meaning, as in
        var a()^[] : ^int;
but the colon is useful so that several related objects can
be declared at once, so
       var a()^[],b(),c[3] : ^int;
declares a as above, b to be a FUNCTION returning a POINTER to
an INTEGER, and c to be an ARRAY of three POINTERS to INTEGERS.

I've always liked the idea. I realize it's unlikely to be
added to C at this late date, but I could hope to see it in
a preprocessor or a new language.

(By the way, I thought I saw this in SIGPLAN Notices several
years ago, but I looked through all my back issues and can't
find it.  Can anyone else supply the reference?)

                - Jim Van Zandt

keesan@bbncca.ARPA (Morris M. Keesan) (01/18/85)

-------------------------------
    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

jrv@Mitre-Bedford (02/10/85)

>     Indeed, changing the syntax of C type declarations at this oint is totally
> infeasible and would break lots of existing programs.  (The preious article
> suggested parsing type declarations from left to right instead f by
> precedence.)  ...
>                            Morris M. Keesan

The change I suggested could be done with only an *addition*
to the syntax - specifically, overloading the ^ operator with
a postfix as well as an infix meaning, and adding a new
declaration syntax.  I don't *think* this would break any
existing programs.
                            - Jim Van Zandt