Tag: Pointer

1 Posts

Function Pointer
Definition [ref]Function Pointer in C[/ref] A function pointer is a pointer to a function. Here's an example showing function pointer declaration and function call. #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(){ // fun_ptr is a pointer to function fun() void (*fun_ptr)(int) = &fun; /* The above line is equivalent of following two void (*fun_ptr)(int); fun_ptr = &fun; */ // Invoking fun() using fun_ptr (*fun_ptr)(10); return 0; } and the example's output: Value of a is 10 Properties[ref]Function Pointer in C[/ref] Unlike normal pointers, a function pointer points to code, not data. Typically a function pointer stores the start of executable code. Unlike normal pointers, we do not allocate de-allocate memory using function pointers.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. #include <stdio.h> // A normal…