LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   A question about function pointer in C... (https://www.linuxquestions.org/questions/programming-9/a-question-about-function-pointer-in-c-894679/)

trist007 07-30-2011 03:34 PM

A question about function pointer in C...
 
What does this do exacty?
Code:

int(*fp)(void) = (int(*)(void))buf
This declares a function pointer that takes a void argument and returns an int. However I don't understand what this assignment does exactly.

ta0kira 07-30-2011 03:58 PM

Quote:

Originally Posted by trist007 (Post 4429493)
What does this do exacty?
Code:

int(*fp)(void) = (int(*)(void))buf
This declares a function pointer that takes a void argument and returns an int. However I don't understand what this assignment does exactly.

int(*)() is a type and int(*fp)() is the way you use it.
Code:

typedef int(*f_pointer)();
f_pointer fp = (f_pointer) buf;

The assignment takes the address of the function so that *fp is a reference (in C++, anyway) to the function, which can be evaluated with ().
Code:

int value = (*fp)(); //normal function-pointer evaluation

int(&fp2)() = *fp; //also allowed in C++
value = fp2();

Kevin Barry

trist007 07-30-2011 04:33 PM

Ah ok I see, thank you.


All times are GMT -5. The time now is 03:14 PM.