472,358 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,358 software developers and data experts.

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 1758
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Asif | last post by:
Hi there, I have been trying to understand the behaviour of char (*pfn)(null) for a couple of days. can some body help me understand the behaviour of char (*pfn)(null) in Visual C++ environment?...
11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
9
by: shaun | last post by:
Dear all, I realized an error in a previous post, I reproduce it here because I'm still not sure how to solve it: I want to make a templated function which points to one-past-the-end of a...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
11
by: Sensei | last post by:
I'm sorry to always bother you guys on simple C topics... but here again I go :) What does the standard C99 (and if you have knowledge, pre-C99 also) say about the behavior of realloc() on...
3
by: nembo kid | last post by:
I have an issue with a simple function that has to make a linear search for a key into an array. If the key is found in the array, the function it has to return 1 to the caller and pass array...
8
by: Jaco Naude | last post by:
Hi I've been struggling with something that should be very simple to solve... Basically, I get a const Obj* from a function and I need to send a pointer to this object to a function accepting...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.