Can anyone pl. explain how the following c program works:
Specifically how function ‘fun’ is assigned to (*p)() = fun; I need to know how compiler compiles this code.
#include<stdio.h>
int fun(); /* function prototype */
int main()
{
int (*p)() = fun;
(*p)();
return 0;
}
int fun()
{
printf("Hello World\n");
return 0;
}
Each function exists in memory somewhere. The statement:
is assigning the memory location of the function fun to p. Then the line:
is calling the function that exists at the memory location that p is pointing to.
The Interweb is full of info on “function pointers.”