Understanding C Function Pointers

poornaMoksha's Avatar author of Understanding C Function Pointers
This is an article on Understanding C Function Pointers in C.
Rated 5.00 By 1 users
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 like this

Light Poster
15Sep2011,08:37   #2
vijaysince89's Avatar
Nice post. But in which kind of situation this will be more helpful.
Ambitious contributor
15Sep2011,11:02   #3
poornaMoksha's Avatar
Do you go through the section 'Where Function Pointers Are Used?' in the above article?
Banned
15Sep2011,17:51   #4
ankitasharma's Avatar
thanks for Sharing it is very useful to me.......
Light Poster
15Sep2011,18:02   #5
vijaysince89's Avatar
Yes I saw it. But my question is that the only purpose of using is this to get the status message alone?
Ambitious contributor
15Sep2011,22:24   #6
poornaMoksha's Avatar
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
Ambitious contributor
15Sep2011,22:25   #7
poornaMoksha's Avatar
Quote:
Originally Posted by ankitasharma View Post
thanks for Sharing it is very useful to me.......
My pleasure
Go4Expert Member
17Sep2011,17:03   #8
colinwood07's Avatar
Thanks share the knowledge.....
Invasive contributor
18Sep2011,14:44   #9
lionaneesh's Avatar
WoaH! Nice to See you back! Nice tutorial I must! Say! All the best and Hoping for more of them!
Ambitious contributor
18Sep2011,17:05   #10
poornaMoksha's Avatar
Sure. Thanks for the appreciation.
Quote:
Originally Posted by lionaneesh View Post
WoaH! Nice to See you back! Nice tutorial I must! Say! All the best and Hoping for more of them!