Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!lll-winken!uunet!kddlab!ccut!titcca!sragwa!wsgw!socslgw!diamond!diamond From: diamond@diamond.csl.sony.junet (Norman Diamond) Newsgroups: comp.lang.c Subject: Re: function casting Message-ID: <10206@socslgw.csl.sony.JUNET> Date: 2 May 89 04:35:18 GMT References: <12481@umn-cs.CS.UMN.EDU> <9266@alice.UUCP> Sender: news@csl.sony.JUNET Reply-To: diamond@csl.sony.junet (Norman Diamond) Organization: /usr/lib/news/organization Lines: 54 In article <12481@umn-cs.CS.UMN.EDU>, clark@umn-cs.CS.UMN.EDU (Robert P. Clark) writes: >> How do I cast something to a pointer to a function that returns >> an integer? In article <9266@alice.UUCP> ark@alice.UUCP (Andrew Koenig) writes: >Best would be to make foo return an appropriate pointer ane >avoid type cheating: > > int (*foo())(); > int (*f)(); > > f = foo(); > >The declaration of foo here says that if you call foo, dereference the >result, and call that, you get an int. The definition of foo would >look something like this: > > int (*foo())() > { > /* ... */ > } > >Don't be too astonished if your compiler balks at this -- but >it is indeed valid C. Mr. Koenig's answers are 100% correct. However, the use of typedef might solve the problem of broken compilers, and maybe even the problem of broken co-workers who have to read your code: typedef int purpose_of_f_t(); typedef purpose_of_f_t *purpose_of_f_ptr_t; typedef purpose_of_f_ptr_t purpose_of_foo_t(); purpose_of_foo_t foo; purpose_of_f_ptr_t f_ptr; int my_int; /* ... */ f_ptr = foo(); /* ... */ my_int = (*f_ptr)(); purpose_of_foo_t foo() { /* ... */ } Norman Diamond, Sony Computer Science Lab (diamond%csl.sony.jp@relay.cs.net) The above opinions are my own. | Why are programmers criticized for If they're also your opinions, | re-inventing the wheel, when car you're infringing my copyright. | manufacturers are praised for it?