469,622 Members | 2,120 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,622 developers. It's quick & easy.

Simple function pointer question


Hello, I am trying to make a simple function that returns a pointer to
another function. In my header file I've declared my function pointer:

void (*pStateFunction) (void); //assume the function pointed to returns
void

and the actual function that returns the pointer:

void* getState(CString myString);

My question is, when returning a function pointer to a function that is
known to be void, is there a more sensible way to do it than define the
return value as a pointer to void, i.e. a pointer to an unknown type? It
seems a bit obscure, which suggests there's a better way to do it.

Thanks,

--
Jeffrey Spoon

Feb 7 '07 #1
4 1628
Jeffrey Spoon wrote:
>
Hello, I am trying to make a simple function that returns a pointer to
another function. In my header file I've declared my function pointer:

void (*pStateFunction) (void); //assume the function pointed to returns
void

and the actual function that returns the pointer:

void* getState(CString myString);

My question is, when returning a function pointer to a function that is
known to be void, is there a more sensible way to do it than define the
return value as a pointer to void, i.e. a pointer to an unknown type? It
seems a bit obscure, which suggests there's a better way to do it.
Actually, returning void* is flawed: there are no conversions between
function pointers and void*. The standard does not even require void* to
have a size that can accommodate for function pointers.

Conversions only exists between function pointers. And the only guarantee
that is given is that converting back and forth gets you back the original
pointer.

What you should do is return the right type to begin with:

typedef void (*pStateFunction_t) (void);

pStateFunction_t getState(CString myString);
Best

Kai-Uwe Bux
Feb 7 '07 #2
In message <eq**********@murdoch.acc.Virginia.EDU>, Kai-Uwe Bux
<jk********@gmx.netwrites

>
What you should do is return the right type to begin with:

typedef void (*pStateFunction_t) (void);

pStateFunction_t getState(CString myString);
That's what I was trying to do, thanks very much.


--
Jeffrey Spoon

Feb 7 '07 #3
Jeffrey Spoon wrote:
...
Hello, I am trying to make a simple function that returns a pointer to
another function. In my header file I've declared my function pointer:

void (*pStateFunction) (void); //assume the function pointed to returns
void

and the actual function that returns the pointer:

void* getState(CString myString);

My question is, when returning a function pointer to a function that is
known to be void, is there a more sensible way to do it than define the
return value as a pointer to void, i.e. a pointer to an unknown type?
Yes, there is. Moreover what you do now is incorrect. Data pointers (like
'void*') and function pointers (like your 'void (*)(void)') are not compatible
with each other. Forcing a conversion between the two leads to undefined behavior.

If you want your function to return a pointer to a function, you just declare it
as such. The "flat" declaration for 'getState' might look as follows

void (*getState(CString myString))(void);

but this is rather difficult to read. A typedef will definitely help in this
case. You can do either

typedef void VoidFunc(void);
VoidFunc* getState(CString myString);

or

typedef void (*VoidFuncPtr)(void);
VoidFuncPtr getState(CString myString);

--
Best regards,
Andrey Tarasevich
Feb 7 '07 #4
In message <12*************@news.supernews.com>, Andrey Tarasevich
<an**************@hotmail.comwrites
>
Yes, there is. Moreover what you do now is incorrect. Data pointers (like
'void*') and function pointers (like your 'void (*)(void)') are not compatible
with each other. Forcing a conversion between the two leads to
undefined behavior.
Thanks Andrey, I completely missed the distinction between the data and
function pointer. Also typedef certainly makes things less confusing.


--
Jeffrey Spoon

Feb 8 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Asif | last post: by
11 posts views Thread by JKop | last post: by
51 posts views Thread by Alan | last post: by
9 posts views Thread by shaun | last post: by
3 posts views Thread by Beta What | last post: by
24 posts views Thread by Michael | last post: by
11 posts views Thread by Sensei | last post: by
3 posts views Thread by nembo kid | last post: by
8 posts views Thread by Jaco Naude | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.