🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3035

Re: typedef void (*generic)()

#3035
From: Norman Diamond
Date: Thu, 17 Dec 1992 01:23
34 lines
1998 bytes
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.
>#include <stdio.h>
>#include <stdlib.h>
>typedef void (*generic)();
>main() {
>        generic f,g;
>        int     i;
>        f = (generic) printf;
>        g = (generic) abs;
>	i=-4;
>	/* 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
>There is also a remark from my compiler on some constructs stating that
>void pointer cannot be cast to function pointers. Is this allowed?

Pointers to object or incomplete types cannot be converted to pointers
to function types, nor vice-versa (except for the special case of
null pointer constants being converted to pointers to function types).

However, any pointer to function type can be converted to any other
pointer to function type.  You have to convert it back before calling
though.  So in this sense, your generic type is indeed generic.

You can do this:
    ((int (*) (char *, ...)) f) ( "some text %d\n",
                                  ((int (*) (int)) g) (i)
                                );
--
Norman Diamond       diamond@jit081.enet.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.
"It's been a lovely recession."
Technical Details
Message-ID: <BzDqIs.1KI@jrd.dec.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!haven.umd.edu!decuac!pa.dec.com!engage.pko.dec.com!e2big.mko.dec.com!jrdzzz.jrd.dec.com!jit533.jit.dec.com!diamond
References: <1992Dec16.172748.24120@bernina.ethz.ch>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c
Path: arc1-ca.pugleaf.net!sparky!uunet!haven.umd.edu!decuac!pa.dec.com!engage.pko.dec.com!e2big.mko.dec.com!jrdzzz.jrd.dec.com!jit533.jit.dec.com!diamond
From: diamond@jit533.jit.dec.com (Norman Diamond)
Subject: Re: typedef void (*generic)()
Message-ID: <BzDqIs.1KI@jrd.dec.com>
Sender: usenet@jrd.dec.com (USENET News System)
Nntp-Posting-Host: jit533.jit.dec.com
Reply-To: diamond@jit.dec.com (Norman Diamond)
Organization: Digital Equipment Corporation Japan , Tokyo
References: <1992Dec16.172748.24120@bernina.ethz.ch>
Date: Thu, 17 Dec 1992 01:23:16 GMT
Lines: 34