How do I declare...

Wayne Throop throopw at rtp47.UUCP
Sat Aug 24 06:41:17 AEST 1985


> Either I'm missing the perfectly obvious or I've found something
> that's impossible to declare in C, even though it makes sense.

You have discovered a fundamental flaw in C type notation.  In essence,
C allows recursive types only in structs, unions, and enums (and I'm not
sure how it would be usefull in enums, so that one can be ruled out for
practical purposes).

Thus, if you want an array of pointers to arrays of pointers, you can't
do it.  Nor can you declare functions returning pointers to functions.
It would be nice to be able to declare a recursive type, like so:

    typedef foo_type (*foo_type)();
    ...
    foo_type foo;
    ...
    foo = (*foo)();

but this just doesn't work (on any compiler I ever heard of).  You can
do something like

    typedef struct foo_struct{ struct foo_struct (*dummy)(); } foo_type;
    ...
    foo_type foo;
    ...
    foo.dummy = (*foo.dummy)();

Another method is less portable but slightly more appealing (in terms of
what is written to declare and use the recursive type).  That is to use
a cast at the appropriate points:

    typedef int (*foo_base_type)();
    typedef foo_base_type (*foo_type)();
    ...
    foo_type foo;
    ...
    foo = (foo_type)(*foo)();

which will fail if your compiler can't cast
pointers-to-functions-returning-int into
pointers-to-functions-returning-pointers-to-functions-returning-int,
which doesn't seem very likely.

In any event, all of these solutions are less pleasing than the
possible alternatives that would exist if only fully recursive type
declaractions were available in C.
-- 
Wayne Throop at Data General, RTP, NC
<the-known-world>!mcnc!rti-sel!rtp47!throopw



More information about the Comp.lang.c mailing list