function casting
Norman Diamond
diamond at diamond.csl.sony.junet
Tue May 2 14:35:18 AEST 1989
In article <12481 at umn-cs.CS.UMN.EDU>, clark at 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 at alice.UUCP> ark at 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 at 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?
More information about the Comp.lang.c
mailing list