#include <stdio.h>
// A normal function with an int parameter
// and void return type
void fun(int a)
{
printf("Value of a is %d\n", a);
}
int main()
{
void (*fun_ptr)(int) = fun; // & removed
fun_ptr(10); // * removed
return 0;
}
Value of a is 10
1. Unlike normal pointers, a function pointer points to code, not data. Typically a function pointer stores the start of the executable code.
2. Unlike normal pointers, we do not allocate de-allocate memory using function pointers.
3. A function?s name can also be used to get functions? address. For example, in the below program, we have removed address operator ?&? in assignment. We have also changed function call by removing *, the program still works.
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.