Wednesday, March 28, 2012
function pointers in C?
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.
Subscribe to:
Post Comments (Atom)
Popular Posts
-
A universally unique identifier ( UUID ) is an identifier standard used in software construction, standardized by the Open...
-
Recently I started working on Japser Studio professional for my new project Cloud to generate the reports. I was very new to all cloud ...
-
Below is C program for AVL Tree implementation. #include<stdio.h> #include<malloc.h> typedef struct bst { int info; int hei...
-
strcmp is another string library function which is used to compare two strings and returns zero if both strings are same , returns +ve valu...
-
One of the complex operation on binary search tree is deleting a node. Insertion is easy by calling recursive insertion. But deletion wont...
-
We have recently faced one tricky issue in AWS cloud while loading S3 file into Redshift using python. It took almost whole day to inde...
-
Object slicing: when a derived class object is assigned to a base class object. only base class data will be copied from derived class and...
-
We have faced lot of weird issues while loading S3 bucke t files into redshift. I will try to explain all issues what we faced. Before go...
-
Below code is to find the cube root of a given integer number with out using pow math library function. Its very simple and brute force...
-
Recently we faced one issue in reading messages from SQS in AWS cloud where we are processing same message multiple times. This issue we...
No comments:
Post a Comment