home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ulowell!news.bbn.com!olivea!charnel!psgrain!ee.und.ac.za!tplinfm
- From: barrett@lucy.ee.und.ac.za (Alan Barrett)
- Newsgroups: comp.lang.c
- Subject: Re: (cast) proposal
- Message-ID: <1k5mnuINNntr@lucy.ee.und.ac.za>
- Date: 27 Jan 93 10:04:46 GMT
- References: <1993Jan26.135214.9306@dxcern.cern.ch>
- Organization: Elec. Eng., Univ. Natal, Durban, S. Africa
- Lines: 62
- NNTP-Posting-Host: lucy.ee.und.ac.za
-
- In article <1993Jan26.135214.9306@dxcern.cern.ch>,
- burow@dxcern.cern.ch (Burkhard Burow) writes:
- > -|>That's why I need a cast for saying:
- > -|> 'This is a pointer to a function.
- > -|> I don't give a flying f*** about the type of argument the
- > ->| function returns.'
-
- As Chris Torek has explained, with his usual eloquence, there is no such
- thing in C.
-
- > RFC: Why doesn't ANSI C provide this?
-
- Because it wouldn't be very useful. An implementation is free to use
- different calling conventions for functions that take different types
- of args or that return different types.
-
- (void(*)()) or (void(*)(void)) is about as close as you can get.
-
- I went back to to your original posting (which had a misleading subject
- line that suggested that it was relevant only to a DEC implementation),
- in which you asked about this:
-
- % My application generates 'wrapper' routines which pass along functions
- % to other routines. The wrapper routines do not know the type of the
- % functions they are passing along.
- %
- % Simplified e.g.
- %
- % void q ( int (*c)() );
- % void __q( void ( *a)() )
- % {
- % q( a );
- % }
-
- The problem here is that the type of the arg that __q passes to q does
- not match the declaration of q. Since you say that the writer of the
- __q function does not know how q is declared, you cannot just cast the
- arg a to (int(*)()) inside __q.
-
- The effect of calling a function through a pointer of the wrong type is
- undefined, but if you can live with that and you know that your C
- implementation permits this dubious practice, then you can say this:
-
- typedef void (*pfv)(); /* pointer to function taking unknown args
- * and returning void */
- typedef int (*pfi)(); /* pointer to function taking unknown args
- * and returning int */
- void q(pfi c); /* q takes a pfi and returns void */
- void __q(pfv a)
- {
- /* We have a pfv that we need to pass to q, but we don't know what
- * type q wants to receive, and we don't know what type q returns.
- * We cheat by casting q into a type that we do know about, and
- * calling the function through the resulting pointer.
- * The behaviour is undefined, but we hope that the implementation
- * is very friendly. */
- ((pfv)q) (a);
- }
-
- --apb
- Alan Barrett, Dept. of Electronic Eng., Univ. of Natal, Durban, South Africa
- RFC822: barrett@ee.und.ac.za
-