devxlogo

Calling a Function Through a Pointer

Calling a Function Through a Pointer

The common way of calling a function through a pointer is using the pointer as if it were a function. In other words, if p is a pointer to a function called f:

 int f(int n){ return n;}int (*p)(int)=f; 

size=3>
Then there’s no syntactic difference between calling f() directly, e.g.,

 int m=f(n);

size=3>
and calling f() through the pointer p:

 int m=p(n);

size=3>
However, it is allowed to use the (*p)(n) to call a function through a pointer:

 int m=(*p)(n);

size=3>
This form was prevalent in pre-standard C and is still permitted today. You shouldn’t use it new code. However, it’s very common in legacy code so you should be familiar with it.

devx-admin

Share the Post: