Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   typecasting of function pointers (http://www.velocityreviews.com/forums/t441886-typecasting-of-function-pointers.html)

srinivas.satish@gmail.com 03-20-2006 05:14 AM

typecasting of function pointers
 
Hi,
is it possible to typecast a function pointer to two different
prototypes.
eg.,
typedef void (functptr1 *) (int , int);
typedef void (functptr2 *) (int);
functptr1 fptr;
fptr = somefunction_name;
fptr(10,20);
fptr = (functptr2)someotherfunction_name;
(functptr2) fptr(10);

I tried the above it did'nt work. If this is wrong please let me know
how it could be done in the right way, also if it is compiler specific,
which compiler(s) permit this.


Michael Mair 03-20-2006 06:23 AM

Re: typecasting of function pointers
 
srinivas.satish@gmail.com schrieb:
> Hi,
> is it possible to typecast a function pointer to two different
> prototypes.
> eg.,
> typedef void (functptr1 *) (int , int);
> typedef void (functptr2 *) (int);
> functptr1 fptr;
> fptr = somefunction_name;
> fptr(10,20);
> fptr = (functptr2)someotherfunction_name;
> (functptr2) fptr(10);
>
> I tried the above it did'nt work. If this is wrong please let me know
> how it could be done in the right way, also if it is compiler specific,
> which compiler(s) permit this.


Please, state exactly what "didn't work" means in future.
Have a look at
<http://www.catb.org/~esr/faqs/smart-questions.html>
And provide the _actual_ code.
The above cannot compile.

So, for the language facilities:
1. Function pointers cannot be converted implicitly (i.e.
without cast). They cannot be explicitly (i.e. via cast)
to another function pointer type in a portable, standard
C way.
This means: Whenever you cast one function pointer type
to another, your program becomes inherently non-portable
and may even break when you use another version of the
same compiler.
2. If you have the same return type, you have good chances
that it will work nonetheless; the right way to do the above
is
void foo(int qux, int quux);
void bar(int baz);

fptr = foo;
fptr(10,20);
fptr = (functptr1) bar;
((functptr2)fptr)(10);
with the typedefs and declarations in place.
However, you can run into deep trouble if calling the wrong
type of function with the wrong number of arguments, so this
is a bad idea at best. If you need different pointer types,
consider using a union of function pointers and some kind of
type marker.
Note: If your parameter list have many parameters (say >5),
then the chance increases that it will not work.
3. On many implementations, even casts between function
pointer types for different return types may work.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.

Flash Gordon 03-20-2006 08:09 PM

Re: typecasting of function pointers
 
Michael Mair wrote:
> srinivas.satish@gmail.com schrieb:
>> Hi,
>> is it possible to typecast a function pointer to two different
>> prototypes.
>> eg.,
>> typedef void (functptr1 *) (int , int);
>> typedef void (functptr2 *) (int);
>> functptr1 fptr;
>> fptr = somefunction_name;
>> fptr(10,20);
>> fptr = (functptr2)someotherfunction_name;
>> (functptr2) fptr(10);
>>
>> I tried the above it did'nt work. If this is wrong please let me know
>> how it could be done in the right way, also if it is compiler specific,
>> which compiler(s) permit this.

>
> Please, state exactly what "didn't work" means in future.
> Have a look at
> <http://www.catb.org/~esr/faqs/smart-questions.html>
> And provide the _actual_ code.
> The above cannot compile.
>
> So, for the language facilities:
> 1. Function pointers cannot be converted implicitly (i.e.
> without cast). They cannot be explicitly (i.e. via cast)
> to another function pointer type in a portable, standard
> C way.
> This means: Whenever you cast one function pointer type
> to another, your program becomes inherently non-portable
> and may even break when you use another version of the
> same compiler.


Not completely true. It is perfectly legal to cast from one function
type to another, it's just that you have to cast it back again before
calling it. There are even times when this is useful, although not very
often.

> 2. If you have the same return type, you have good chances
> that it will work nonetheless; the right way to do the above
> is
> void foo(int qux, int quux);
> void bar(int baz);
>
> fptr = foo;
> fptr(10,20);
> fptr = (functptr1) bar;
> ((functptr2)fptr)(10);
> with the typedefs and declarations in place.
> However, you can run into deep trouble if calling the wrong
> type of function with the wrong number of arguments, so this
> is a bad idea at best. If you need different pointer types,
> consider using a union of function pointers and some kind of
> type marker.
> Note: If your parameter list have many parameters (say >5),
> then the chance increases that it will not work.


I would strongly advise against doing this even if it appears to work.
If you do not always use all the parameters then there are two simple
ways to deal with it:
1) Pass suitable 0 values (or some other place marker) for unused
parameters.
2) Write a function that takes a variable number of arguments using the
mechanism C provides for doing this.

> 3. On many implementations, even casts between function
> pointer types for different return types may work.


However, even if it works you should still not do it.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Michael Mair 03-20-2006 08:44 PM

Re: typecasting of function pointers
 
Flash Gordon schrieb:
> Michael Mair wrote:
>> srinivas.satish@gmail.com schrieb:
>>
>>> Hi,
>>> is it possible to typecast a function pointer to two different
>>> prototypes.
>>> eg.,
>>> typedef void (functptr1 *) (int , int);
>>> typedef void (functptr2 *) (int);
>>> functptr1 fptr;
>>> fptr = somefunction_name;
>>> fptr(10,20);
>>> fptr = (functptr2)someotherfunction_name;
>>> (functptr2) fptr(10);
>>>
>>> I tried the above it did'nt work. If this is wrong please let me know
>>> how it could be done in the right way, also if it is compiler specific,
>>> which compiler(s) permit this.

>>
>> Please, state exactly what "didn't work" means in future.
>> Have a look at
>> <http://www.catb.org/~esr/faqs/smart-questions.html>
>> And provide the _actual_ code.
>> The above cannot compile.
>>
>> So, for the language facilities:
>> 1. Function pointers cannot be converted implicitly (i.e.
>> without cast). They cannot be explicitly (i.e. via cast)
>> to another function pointer type in a portable, standard
>> C way.
>> This means: Whenever you cast one function pointer type
>> to another, your program becomes inherently non-portable
>> and may even break when you use another version of the
>> same compiler.

>
> Not completely true. It is perfectly legal to cast from one function
> type to another, it's just that you have to cast it back again before
> calling it. There are even times when this is useful, although not very
> often.


Thanks for pointing that out.
For the record: C99, 6.3.2.3#8


>> 2. If you have the same return type, you have good chances
>> that it will work nonetheless; the right way to do the above
>> is
>> void foo(int qux, int quux);
>> void bar(int baz);
>>
>> fptr = foo;
>> fptr(10,20);
>> fptr = (functptr1) bar;
>> ((functptr2)fptr)(10);
>> with the typedefs and declarations in place.
>> However, you can run into deep trouble if calling the wrong
>> type of function with the wrong number of arguments, so this
>> is a bad idea at best. If you need different pointer types,
>> consider using a union of function pointers and some kind of
>> type marker.
>> Note: If your parameter list have many parameters (say >5),
>> then the chance increases that it will not work.

>
> I would strongly advise against doing this even if it appears to work.
> If you do not always use all the parameters then there are two simple
> ways to deal with it:
> 1) Pass suitable 0 values (or some other place marker) for unused
> parameters.
> 2) Write a function that takes a variable number of arguments using the
> mechanism C provides for doing this.


Note: The former may not the natural thing to do; sometimes the
union-based approach is more helpful. The latter, especially
with key/value pairs, does not suffer from that but may make for
troublesome parameter combination checking.

>> 3. On many implementations, even casts between function
>> pointer types for different return types may work.

>
> However, even if it works you should still not do it.


Indeed :-)


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.

CBFalconer 03-20-2006 11:31 PM

Re: typecasting of function pointers
 
Flash Gordon wrote:
> Michael Mair wrote:
>

.... snip ...
>
>> Note: If your parameter list have many parameters (say >5),
>> then the chance increases that it will not work.

>
> I would strongly advise against doing this even if it appears to
> work. If you do not always use all the parameters then there are
> two simple ways to deal with it:
> 1) Pass suitable 0 values (or some other place marker) for
> unused parameters.
> 2) Write a function that takes a variable number of arguments
> using the mechanism C provides for doing this.


I advise against 2). Variadic functions do not provide proper
parameter checking, and are the source of many mistakes. They may
be convenient, but can always be replaced by something.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>



Ben Pfaff 03-21-2006 12:59 AM

Re: typecasting of function pointers
 
CBFalconer <cbfalconer@yahoo.com> writes:

> Flash Gordon wrote:
>> 2) Write a function that takes a variable number of arguments
>> using the mechanism C provides for doing this.

>
> I advise against 2). Variadic functions do not provide proper
> parameter checking, and are the source of many mistakes. They may
> be convenient, but can always be replaced by something.


If you use a compiler that can check variadic function arguments,
then you gain some protection. For example, GCC can check
printf()-style variadic function arguments; newer versions can
also check that an argument list is terminated by a null pointer.
You don't even have to *always* compile with GCC to get some help
from this feature; as long as you sometimes compile with GCC, it
will point out (most of) your mistakes.
--
Bite me! said C.

Flash Gordon 03-21-2006 01:00 AM

Re: typecasting of function pointers
 
Michael Mair wrote:
> Flash Gordon schrieb:
>> Michael Mair wrote:


<snip stuff agreed>

>>> 2. If you have the same return type, you have good chances
>>> that it will work nonetheless; the right way to do the above
>>> is
>>> void foo(int qux, int quux);
>>> void bar(int baz);
>>>
>>> fptr = foo;
>>> fptr(10,20);
>>> fptr = (functptr1) bar;
>>> ((functptr2)fptr)(10);
>>> with the typedefs and declarations in place.
>>> However, you can run into deep trouble if calling the wrong
>>> type of function with the wrong number of arguments, so this
>>> is a bad idea at best. If you need different pointer types,
>>> consider using a union of function pointers and some kind of
>>> type marker.
>>> Note: If your parameter list have many parameters (say >5),
>>> then the chance increases that it will not work.

>>
>> I would strongly advise against doing this even if it appears to work.
>> If you do not always use all the parameters then there are two simple
>> ways to deal with it:
>> 1) Pass suitable 0 values (or some other place marker) for unused
>> parameters.
>> 2) Write a function that takes a variable number of arguments using the
>> mechanism C provides for doing this.

>
> Note: The former may not the natural thing to do; sometimes the
> union-based approach is more helpful. The latter, especially
> with key/value pairs, does not suffer from that but may make for
> troublesome parameter combination checking.


I agree there are many other approaches. I was just pointing out a
couple of the simplest to implement which don't rely on doing odd stuff
with function pointers, starting with the very simplest.

<snip more stuff agreed>
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

CBFalconer 03-21-2006 02:22 AM

Re: typecasting of function pointers
 
Ben Pfaff wrote:
> CBFalconer <cbfalconer@yahoo.com> writes:
>> Flash Gordon wrote:

>
>>> 2) Write a function that takes a variable number of arguments
>>> using the mechanism C provides for doing this.

>>
>> I advise against 2). Variadic functions do not provide proper
>> parameter checking, and are the source of many mistakes. They may
>> be convenient, but can always be replaced by something.

>
> If you use a compiler that can check variadic function arguments,
> then you gain some protection. For example, GCC can check
> printf()-style variadic function arguments; newer versions can
> also check that an argument list is terminated by a null pointer.
> You don't even have to *always* compile with GCC to get some help
> from this feature; as long as you sometimes compile with GCC, it
> will point out (most of) your mistakes.


That's with a constant format string, and for functions that are
defined in the standard. None of which applies to Flashs suggestion
2.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>



Ben Pfaff 03-21-2006 04:51 AM

Re: typecasting of function pointers
 
CBFalconer <cbfalconer@yahoo.com> writes:

> Ben Pfaff wrote:
>> For example, GCC can check
>> printf()-style variadic function arguments; newer versions can
>> also check that an argument list is terminated by a null pointer.
>> You don't even have to *always* compile with GCC to get some help
>> from this feature; as long as you sometimes compile with GCC, it
>> will point out (most of) your mistakes.

>
> That's with a constant format string, and for functions that are
> defined in the standard.


You are wrong about the latter: you can apply these to any
function to you like, using a GCC extension. And, as I said, you
get most of the benefit even if you don't always use GCC.

> None of which applies to Flashs suggestion 2.


The null sentinel check might work, or it might not.
--
"I don't have C&V for that handy, but I've got Dan Pop."
--E. Gibbons

Jordan Abel 03-21-2006 05:14 AM

Re: typecasting of function pointers
 
On 2006-03-21, CBFalconer <cbfalconer@yahoo.com> wrote:
> Ben Pfaff wrote:
>> CBFalconer <cbfalconer@yahoo.com> writes:
>>> Flash Gordon wrote:

>>
>>>> 2) Write a function that takes a variable number of arguments
>>>> using the mechanism C provides for doing this.
>>>
>>> I advise against 2). Variadic functions do not provide proper
>>> parameter checking, and are the source of many mistakes. They may
>>> be convenient, but can always be replaced by something.

>>
>> If you use a compiler that can check variadic function arguments,
>> then you gain some protection. For example, GCC can check
>> printf()-style variadic function arguments; newer versions can
>> also check that an argument list is terminated by a null pointer.
>> You don't even have to *always* compile with GCC to get some help
>> from this feature; as long as you sometimes compile with GCC, it
>> will point out (most of) your mistakes.

>
> That's with a constant format string, and for functions that are
> defined in the standard. None of which applies to Flashs suggestion
> 2.


Or, via __attribute__'s, for a format string returned by a function
guaranteed to return a format string equivalent to its argument, and for
any function which accepts printf-like or scanf-like format strings.


All times are GMT. The time now is 05:39 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.