home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.std.c:3243 comp.lang.c:18726
- Path: sparky!uunet!gossip.pyramid.com!olivea!charnel!sifon!thunder.mcrcim.mcgill.edu!mouse
- From: mouse@thunder.mcrcim.mcgill.edu (der Mouse)
- Newsgroups: comp.std.c,comp.lang.c
- Subject: Re: typedef void (*generic)()
- Message-ID: <1992Dec21.001537.12419@thunder.mcrcim.mcgill.edu>
- Date: 21 Dec 92 00:15:37 GMT
- References: <1992Dec16.172748.24120@bernina.ethz.ch>
- Followup-To: comp.lang.c
- Organization: McGill Research Centre for Intelligent Machines
- Lines: 39
-
- In article <1992Dec16.172748.24120@bernina.ethz.ch>, hoesel@igc.ethz.ch (Frans van Hoesel) writes:
-
- > I have a problem in doing what I want to do in c.
-
- > typedef void (*generic)();
-
- > f = (generic) printf;
- > g = (generic) abs;
-
- > /* and now do something like :printf("some text %d\n",abs(i)
- > ), but using f and g instead !*/
-
- > This isn't the right group to ask (I know), but propably the best
-
- Asking elsewhere than in the right group is never best. I'm taking
- steps to move it to comp.lang.c, since it really is not a standard
- question, but a generic C question.
-
- As for how you'd do it,
-
- (*(int (*)(const char *, ...))f)("some text %d\n",(*(int (*)(int))g)(i));
-
- Note the casts on the functions! The compiler may be using different
- calling sequences depending on the argument pattern and return type, so
- for the calls to work you must cast the pointers correctly. (The two
- "(*(" sequences may be replaced by "((" instead; I just happen to like
- to follow my function pointers before calling them.)
-
- > There is also a remark from my compiler on some constructs stating
- > that void pointer cannot be cast to function pointers. Is this
- > allowed?
-
- Only in the special case of a null pointer constant. Object pointers
- and function pointers are not, in general, interconvertible.
-
- der Mouse
-
- mouse@larry.mcrcim.mcgill.edu
-