[net.unix-wizards] C syntax

edhall%rand-unix@sri-unix.UUCP (08/19/83)

Try:

static void (*p[N])();


In general, remember that modifiers to a given type (that is,
(), *, and []) are parsed right-to-left, successively modifying
the base type of the item.  Parentheses can change this order.
Thus, the above declaration parses:

    [N]         Array of N
    *           Pointers to
    ()          Functions returning
    void        type `void',
    static      storage class `static'.

Another toughie would be an array of pointers to functions returning
strings:

char *(p[N])();

With a bit of practice, though, creating data types in C is about
as simple as any other language.  Some complain that C is `backwards'
from languages like PASCAL.  This is because the C declaration was
designed to be an example of the use of the object declared; thus,
using the second declaration for `p', above, the value of `x' in:

	      x = *(p[n])();

is of type `char'.  This `declaration by example' is more of a feature
than a obstacle.

		-Ed Hall
		Rand Corporation

clark.wbst@PARC-MAXC.ARPA (08/19/83)

The VAX-11 C book has an excellent explaination on figuring out such things.

--Ray Clark

mann%Shasta@su-score@sri-unix.UUCP (08/25/83)

From:  Tim Mann <mann%Shasta@su-score>

The proper syntax for a typedef that defines a type "pointer to
function returning void" would be

	typedef void (*P_ADDR)();

The next trick is to figure out how to declare a static array of these
pointers, WITHOUT using the above typedef.  That one had me tearing my
hair until I gave up and used the typedef.

    --Tim

romkey%mit-csr@sri-unix.UUCP (08/26/83)

From:  John L. Romkey <romkey@mit-csr>

          I believe that the msg about types from Ed Hall had a bug in it. The
second example was an array of pointers to functions returning strings, and
Ed used

char *(p[N])();

as the declaration. But this is actually an array of functions returning
strings, which shouldn't mean much to most C compilers. What you really need
here is another *, as in:

char *(*p[N])();

and when calling one of these functions, you want to say:

          x = *(*p[n])();

I still remember the awful pain of trying to figure out how to declare a
function which returned a pointer to a function which returned an int.

          - John Romkey
            romkey@mit-csr

edhall%rand-unix@sri-unix.UUCP (08/26/83)

Sorry about that; it was after midnight and I wanted to get home.
You're quite right about the missing `*'.

		-Ed