functions returning pionters to func - (nf)
shah at fortune.UUCP
shah at fortune.UUCP
Sun Dec 18 02:05:02 AEST 1983
#R:azure:-243000:fortune:26900008:000:1551
fortune!shah Dec 17 02:47:00 1983
+------
| fortune:net.unix / rpw3 / 12:16 am Dec 17, 1983
| ....
| I finally solved it using typedefs, but wasn't happy, since there seemed
| something inelegant about that. The following program does it WITHOUT
| typedefs (although the typedef version is easier to read, and was what
| was used in production).
| ....
| struct foo { struct foo (*(*dummy)())[]; } (*state)[];
| ....
+-------
Something that makes reading (comprehension) easy seems
more elegant to me. Coming up with the right struct
definition without using typedefs IS more challenging (even
reading it is challenging, every time!) but elegant it is not.
Like routines typedefs are a factoring mechanism. Unlike
routines they cost you nothing at runtime. I recommend
everyone to use them where appropriate; they are free.
The key concept in Rob's problem is how to define a
recursive type involving a routine ptr. Here is a simpler
problem with that property:
Let f be a pointer to a routine. Define f so that the
result of the routine pointed to by f can be assigned to
f. The assignment must be typesafe and must not use any
casts.
Try to solve this with typedefs and then without. The
compiled code of a correct solution will be identical to code
of
typedef char * (*fType)();
fType f;
f = (fType) (*f());
Please do not post any solution(s) to net -- not too soon,
anyway. Anyone else have REALLY tough C puzzles?
-- Bakul Shah
More information about the Comp.unix
mailing list