Re: Casting function pointers
- From: Hans-Bernhard Bröker <HBBroeker@xxxxxxxxxxx>
- Date: Wed, 30 Jan 2008 23:05:40 +0100
KKL wrote:
I wonder how casting with function pointers work, how it "should be"
handled and how it "should not be" handled.
That's actually quite simply: it "should not" be handled at all.
Whenever you think you should cast a function pointer, the probability is very high that you're trying to do the wrong thing, so it's generally best to not do it at all.
The only safe casts involving function pointers are those to other function pointer types --- and that only if you cast back to the original type before actually using the pointer to call the function. But if you must eventually go back to the original type anyway, what would be the point of casting in the first place?
Module: Task Service Abstraction Layer
int taskCreate(char* taskName, void* funcPtr, int
taskPriority,..........)
{
//Casting void* funcPtr to function pointer that returns void and
accepts no arguments
TaskProperties.entryPtr = (void ()(void)* ) funcPtr;
This is an invitation for disaster. Function pointers and data pointers don't mix. Not even if the data pointers are (void *).
And on top of that, your function pointer cast looks syntactically wrong. It should be (void (*)(void)).
.
- References:
- Casting function pointers
- From: KKL
- Casting function pointers
- Prev by Date: Re: C programming on ARM
- Next by Date: Re: Microchip's PIC32 : comments needed
- Previous by thread: Re: Casting function pointers
- Next by thread: Re: Casting function pointers
- Index(es):
Relevant Pages
|