Facts about function pointers

C Programming Language Pointer in C Language (Article) Pointer in C Language (Program)

853

Program:

#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;
}

Output:

Value of a is 10

Explanation:

some interesting facts about function pointers

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.


This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.