What is function pointer in c ?
Function pointer is a type of pointer by which we can point to a function as we point a ordinary variable using pointer .
Every function has an address in memory and we can point to that function address but as we know pointer which will point to function should be same type (function type)
Every function has an address in memory and we can point to that function address but as we know pointer which will point to function should be same type (function type)
here i am trying to point address of PRINTF function .
#include<stdio.h>
main()
{
int (*func_point)(char const *,...) ;
func_point=&printf; // assigning the address of printf to "func_point" function
// now we "func_point" as printf function
func_point("THIS IS FUNCTION POINTER EXAMPLE");
}
In first line we declaring a function pointer as printf type .........because we have to point the function printf.
now in next line just assigning the address of printf to func_point (i.e. func_point has address of printf now )
and so now we can use the func_point as printf . :)
A strange thing you might face in first line if you doesn't know more about the printf
int (*func_point)(char const*,...) ...it's just prototype of printf ,
Since we have to point the function printf so we have to make the funcction pointer as same as the function.
0 comments: