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,
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.
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!
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Declare a C/C++ function returning pointer to array of integer pointers
- Double Pointer (Pointer to Pointer) in C
- Different ways to declare variable as constant in C and C++
- Pointer to an Array | Array Pointer
- What is a Pointer to a Null pointer
- Function Pointer in C
- How to return a Pointer from a Function in C
- Pointer vs Array in C
- 'this' pointer in C++
- Type of 'this' pointer in C++
- Multidimensional Pointer Arithmetic in C/C++
- When do we pass arguments by reference or pointer?
- Scope Resolution Operator Versus this pointer in C++?
- Opaque Pointer
- Passing by pointer Vs Passing by Reference in C++
- void pointer in C / C++
- NULL pointer in C
- C | Pointer Basics | Question 1
- C | Pointer Basics | Question 2
- C | Pointer Basics | Question 3