Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Understanding C Function Pointers (http://www.go4expert.com/articles/understanding-c-function-pointers-t26662/)

poornaMoksha 11Sep2011 16:41

Understanding C Function Pointers
 
Function pointers are pointers that can hold the address of a particular type of functions. These pointers, once initialized can be used to call the function whose address they are pointing to.

Declaring Function Pointer



A function pointer can be declared in following way :

<return type of the function being pointed to> (*<name of func ptr>)(<expected arguments by the function being pointed to>)

For example, if we have a function 'int func(char)', then a function pointer to this function would be dclared as:

int(*func_ptr)(char)

So, here 'func_ptr' is the name of the function pointer which can point to a funtion that expects a 'char' as argument and returns an 'int'

Initializing & Using Function Pointer



Continuing with the above example, the func_ptr can be initialized as follows :
Code:

func_ptr = func; // the name of any function represents the address of function in memory.
func_ptr = &func; // This is also valid and same as the above line.

and can be used as :
Code:

int ret = fun_ptr('a'); // Since now func_pre holds the address of 'func', This line calls the function 'func' with argument 'a'
Example
Code:

#include<stdio.h>
#include<string.h>

int func(char c)
{
        printf("\n [%c] \n",c);
        return 0;
}

int main(void)
{
        int(*func_ptr)(char); // Declare a function pointer
        func_ptr = func; // store the address of 'func' in 'func_ptr'
        return func_ptr('a'); // Calling 'func' through 'func_ptr'
}

Where Function Pointers Are Used?



Function pointers are used mainly in callback mechanisms. A callback mechanism usually comes into picture when an application is using some library. Some functions defined in library expect a function pointer from application using which the library functions can send some error or status message to the application to keep the application informed of whats going inside.

For example, I have created an example to simulate the above stated scenario. Here the function 'divide()' can be thought of as a library function(though I have defined it in the same file for simplicity). The application registers the function 'func' as a callback to receive the status messages from the function 'divide()'.
Code:

#include<stdio.h>
#include<string.h>

int func(char *s)
{
        printf("Callback recieved : %s",s);
        return 0;
}

float divide(unsigned int a, unsigned int b, int(*status_callback)(char*))
{
        status_callback("\n Recieved values...validating\n");
        if(b == 0)
        {
                status_callback("\n b = 0 detected, aborting....\n");
                return -1;
        }
        status_callback("\n Validation complete, performing division\n");
        float ret = a/b;
        status_callback("\n Division complete, returning value\n");
        return ret;
}


int main(void)
{
        int(*func_ptr)(char*);
        unsigned int a = 6, b = 3;
        printf("\n This is a prgram to find a/b\n");

        func_ptr = func;

        float ret = divide(a,b,func_ptr);
        printf("\n divide returned [%f]\n",ret);

        return 0;

}

The output of the above program is as :

Code:

This is a prgram to find a/b
Callback recieved :
 Recieved values...validating
Callback recieved :
 Validation complete, performing division
Callback recieved :
 Division complete, returning value

 divide returned [2.000000]

Secret Tip for Function Pointer



We can make a particular function pointer a type during declaration using 'typedef'

typedef int(*func_ptr)(char);

So the above line makes 'func_ptr' as a type (just like we have int, char etc as types) and we can declare variables of this type

func_ptr ptr1;

So the above lines creates a ptr1 of type func_ptr, hence ptr1 becomes a function pointer of type func_ptr.

vijaysince89 15Sep2011 08:37

Re: Understanding C Function Pointers
 
Nice post. But in which kind of situation this will be more helpful.

poornaMoksha 15Sep2011 11:02

Re: Understanding C Function Pointers
 
Do you go through the section 'Where Function Pointers Are Used?' in the above article?

ankitasharma 15Sep2011 17:51

Re: Understanding C Function Pointers
 
thanks for Sharing it is very useful to me.......

vijaysince89 15Sep2011 18:02

Re: Understanding C Function Pointers
 
Yes I saw it. But my question is that the only purpose of using is this to get the status message alone?

poornaMoksha 15Sep2011 22:24

Re: Understanding C Function Pointers
 
Well, that was just an example...Another possible use could be in the following kind of functions..
http://pubs.opengroup.org/onlinepubs...ad_create.html

poornaMoksha 15Sep2011 22:25

Re: Understanding C Function Pointers
 
Quote:

Originally Posted by ankitasharma (Post 86986)
thanks for Sharing it is very useful to me.......

My pleasure :smug:

colinwood07 17Sep2011 17:03

Re: Understanding C Function Pointers
 
Thanks share the knowledge.....

lionaneesh 18Sep2011 14:44

Re: Understanding C Function Pointers
 
WoaH! Nice to See you back! Nice tutorial I must! Say! :D All the best and Hoping for more of them!

poornaMoksha 18Sep2011 17:05

Re: Understanding C Function Pointers
 
Sure. Thanks for the appreciation.:pleased:
Quote:

Originally Posted by lionaneesh (Post 87069)
WoaH! Nice to See you back! Nice tutorial I must! Say! :D All the best and Hoping for more of them!



All times are GMT +5.5. The time now is 03:56.