Google News
logo
Embedded C - Interview Questions
How to declare a pointer to a function in C?
The syntax for declaring function pointer is very straightforward. It seems difficult in beginning but once you are familiar with function pointer then it becomes easy.
 
The declaration of a pointer to a function is similar to the declaration of a function. That means the function pointer also requires a return type, declaration name, and argument list. One thing that you need to remember here is, whenever you declare the function pointer in the program then the declaration name is preceded by the * (Asterisk) symbol and enclosed in parenthesis.
 
For example : 
void ( *fpData )( int );

 

For a better understanding, let’s take an example to describe the declaration of a function pointer in the C program.
Ex: 
void ( *pfDisplayMessage) (const char *);
In the above expression, pfDisplayMessage is a pointer to a function taking one argument, const char *, and returns void.
 
When we declare a pointer to function in c then there is a lot of importance of the bracket. If in the above example, I remove the bracket, then the meaning of the above expression will be change and it becomes void *pfDisplayMessage (const char *). It is a declaration of a function that takes the const character pointer as arguments and returns a void pointer.
Advertisement