function pointer is a pointer variable, just like a normal pointer. but function pointer points to the address of the function. In C like all normal variables, functions also will be stored in the memory. So every function will have valid address. function pointer will point to this address.
Sample code to display address of a function:
Output:
address of dis() function is 4004d8
How to declare a function pointer: declaring a function pointer looks little confusing. But if you understand the concept of the function pointer, it will be very easy. Lets take declaration of a normal pointer variable, we will do it by using *(asterisk) with variable name. Similarly for function pointer also , we need to use * with function pointer name with in the braces. If you not enclosed in braces, it is not a function pointer.
Syntax:
Calling a function using function pointer: Calling a function using function pointer is like a normal function call only. you can use function pointer or function name, both works in a same way.
Complete working Example:
Output:
sum is 5
sum is 8
Uses of funtion pointers: This will be usefull for call back functions, where the calling definition changes dynamically. This is similar to runtime binding or late binding in C++ which can be implemented using virtual functions. For example take the implementation of a qsort() library function in Unix. from the man pages prototype is given below.
In the above qsort prototype, third argument is a function pointer to 'compar' function, which takes two void pointers as arguments and returns integer value. Compar function works for any data type, and it return zero if both are equl , return 1 if first one is greater than second and returns -1 if second argument is greater.
Sample code to display address of a function:
void dis() { printf("Demo for printing function address \n"); } main() { printf("address of dis() function is %x\n",dis); }
Output:
address of dis() function is 4004d8
How to declare a function pointer: declaring a function pointer looks little confusing. But if you understand the concept of the function pointer, it will be very easy. Lets take declaration of a normal pointer variable, we will do it by using *(asterisk) with variable name. Similarly for function pointer also , we need to use * with function pointer name with in the braces. If you not enclosed in braces, it is not a function pointer.
Syntax:
// declaring the function pointer which takes zero arguments and returns void void (*funcPtr)(); //this is not a function pointer, because there are no braces. This function will take zero arguments and returns void pointer. void *funcPtr();Assigning function pointer: Assigning a function address to a function pointer is like a normal assignment. But signature of the function should match to the function pointer. i.e no. arguments and return type should match the signature. while assinging the fucntion address to the function pointer ampersand(&) is optional.
//function for adding two int's int int_add(int x, int y) { return (x+y); } //function pointer declaration int (*fncPtr)(int, int); //assigning fncPtr = &int_add; // this is also valid fncPtr = int_add;
Calling a function using function pointer: Calling a function using function pointer is like a normal function call only. you can use function pointer or function name, both works in a same way.
// calling normal function int sum = int_add(2,3); // calling function using function pointer int sum = fncPtr(2,3)
Complete working Example:
int int_add(int x, int y) { return (x+y); } //function pointer declaration int (*fncPtr)(int, int); main() { fncPtr = &int_add; //assigning the fucntion address to pointer int sum = fncPtr(2,3); // calling function using fuction pointer printf("sum is %d\n",sum); sum = int_add(5,3); // callint normal function printf("sum is %d\n",sum); }
Output:
sum is 5
sum is 8
Uses of funtion pointers: This will be usefull for call back functions, where the calling definition changes dynamically. This is similar to runtime binding or late binding in C++ which can be implemented using virtual functions. For example take the implementation of a qsort() library function in Unix. from the man pages prototype is given below.
void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *));
In the above qsort prototype, third argument is a function pointer to 'compar' function, which takes two void pointers as arguments and returns integer value. Compar function works for any data type, and it return zero if both are equl , return 1 if first one is greater than second and returns -1 if second argument is greater.
No comments:
Post a Comment