How to declare a pointer to a function?
Well, we assume that you know what does it mean by pointer in C. So how do we create a pointer to an integer in C?
Huh..it is pretty simple..
int * ptrInteger; /*We have put a * operator between int
and ptrInteger to create a pointer.*/
Here ptrInteger is a pointer to integer. If you understand this, then logically we should not have any problem in declaring a pointer to a function 🙂
So let us first see ..how do we declare a function? For example,
int foo(int);
Here foo is a function that returns int and takes one argument of int type. So as a logical guy will think, by putting a * operator between int and foo(int) should create a pointer to a function i.e.
int * foo(int);
But Oops..C operator precedence also plays role here ..so in this case, operator () will take priority over operator *. And the above declaration will mean – a function foo with one argument of int type and return value of int * i.e. integer pointer. So it did something that we didn’t want to do. 🙁
So as a next logical step, we have to bind operator * with foo somehow. And for this, we would change the default precedence of C operators using () operator.
int (*foo)(int);
That’s it. Here * operator is with foo which is a function name. And it did the same that we wanted to do.
So that wasn’t as difficult as we thought earlier!
Recommended Posts:
- Declare a C/C++ function returning pointer to array of integer pointers
- Function Pointer in C
- Different ways to declare variable as constant in C and C++
- Double Pointer (Pointer to Pointer) in C
- 'this' pointer in C++
- C++ | this pointer | Question 4
- C++ | this pointer | Question 5
- Pointer to an Array | Array Pointer
- A C/C++ Pointer Puzzle
- Opaque Pointer
- Type of 'this' pointer in C++
- C++ | this pointer | Question 3
- Pointer vs Array in C
- C++ | this pointer | Question 1
- NULL pointer in C



