1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Understanding C Function Pointers

Discussion in 'C' started by poornaMoksha, Sep 11, 2011.

  1. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    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.
     
    lionaneesh likes this.
  2. vijaysince89

    vijaysince89 New Member

    Joined:
    Sep 8, 2011
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    India
    Home Page:
    Nice post. But in which kind of situation this will be more helpful.
     
  3. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    Do you go through the section 'Where Function Pointers Are Used?' in the above article?
     
  4. ankitasharma

    ankitasharma Banned

    Joined:
    Jul 23, 2011
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    thanks for Sharing it is very useful to me.......
     
  5. vijaysince89

    vijaysince89 New Member

    Joined:
    Sep 8, 2011
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    India
    Home Page:
    Yes I saw it. But my question is that the only purpose of using is this to get the status message alone?
     
  6. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
  7. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    My pleasure :smug:
     
  8. colinwood07

    colinwood07 New Member

    Joined:
    Sep 15, 2011
    Messages:
    29
    Likes Received:
    1
    Trophy Points:
    0
    Thanks share the knowledge.....
     
  9. lionaneesh

    lionaneesh New Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    India
    WoaH! Nice to See you back! Nice tutorial I must! Say! :D All the best and Hoping for more of them!
     
  10. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    Sure. Thanks for the appreciation.:pleased:
     
  11. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    You are welcome buddy!!!
     
  12. hanleyhansen

    hanleyhansen New Member

    Joined:
    Jan 24, 2008
    Messages:
    336
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Drupal Developer/LAMP Developer
    Location:
    Clifton
    Home Page:
    Great info! Pointers are huge in Objective C which is the language of iOS. I just recently started iOS programming and it is interesting to see how all objects are allocated on the heap which makes pointers a big part of Objective C. Great post!
     
  13. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    Thanks for appreciation :nice:
     
  14. jhon786

    jhon786 New Member

    Joined:
    Oct 12, 2011
    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for shearing this useful information. Will you please tell me something about inline functions.. looking forward for you reply.
     

Share This Page