473,385 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1836
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.