By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,905 Members | 1,630 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,905 IT Pros & Developers. It's quick & easy.

Simple function pointer question

P: n/a

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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.