Skip to content
Related Articles

Related Articles

How to declare a pointer to a function?
  • Difficulty Level : Easy
  • Last Updated : 28 May, 2017

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? 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 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 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!

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.
My Personal Notes arrow_drop_up
Recommended Articles
Page :