in Technology by
How to declare a pointer to a function in C?

1 Answer

0 votes
by

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.
e.g,

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.

Related questions

+1 vote
    Which built-in function can be used to move the file pointer internally in C Programming?...
asked Nov 9, 2020 in Technology by JackTerrance
+1 vote
    What is a pointer to a function? Give the general syntax for the same in C Programming?...
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    Can we declare a variable be both constant and volatile in C?...
asked Jan 23, 2021 by JackTerrance
0 votes
    How can we do Pointer comparison in C?...
asked Jan 24, 2021 in Technology by JackTerrance
0 votes
    How is a Program increment a pointer in C?...
asked Jan 24, 2021 in Technology by JackTerrance
0 votes
    What is Indirection and Increment/Decrement operators with a pointer in C?...
asked Jan 24, 2021 in Technology by JackTerrance
0 votes
    Which type of pointer is the most convenient way of storing the raw address in C programming?...
asked Jan 23, 2021 in Technology by JackTerrance
0 votes
    What is the use of a double pointer (pointer to pointer) in C?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    What is the advantage of a void pointer in C?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    What is the usage of the NULL pointer in C?...
asked Jan 21, 2021 in Technology by JackTerrance
0 votes
    What is the size of a void pointer in C?...
asked Jan 21, 2021 in Technology by JackTerrance
0 votes
    What is a near pointer in C?...
asked Jan 21, 2021 in Technology by JackTerrance
0 votes
    What is a far pointer in C?...
asked Jan 21, 2021 in Technology by JackTerrance
0 votes
    What is the usage of the pointer in C?...
asked Jan 21, 2021 in Technology by JackTerrance
0 votes
    What is dangling pointer in C?...
asked Jan 21, 2021 in Technology by JackTerrance
...