A C pointer is a variable that holds the address of another variable. Can it hold the address of a function? The answer is YES. You can declare a pointer variable and store the address of a function in this variable.
int add(int,int);
int *funptr=&add;
Or you can also assign a function name to the pointer instead.
int *funptr=add;
In C, a function name acts as a constant pointer to the function, so you can omit the & (address operator).
One of the benefits is that the same pointer variable can call different functions depending on which function it is pointing to. Using a function pointer, you can call the functions dynamically. It is useful when you want to pass a function as an argument. Here is an example:
No comments:
Post a Comment