Friday 9 September 2011

Function Pointers in C

Hi all. This is my first post. I am going to write about function pointers today. Question that will come in your mind are;

  • What are function pointers?
  • Why do we need that?
  • How to implement them?

Ok so I will try to answer them one by on.

What are Function Pointer?


Well function pointers are pointer to a function. As simple as that :P. No seriously its true! It stores address of a function. As we know every program in execution has some memory allocated to it. Obviously that memory has an address. So if we call that address (from somewhere :p) we are actually calling the program at that address. Confused??? if not go to next topic. Let me give you a weird real world example. Mr. X (a function) has phone no. xxxxxxx(address to it). So calling Mr.X directly would be same as you call the person at  xxxxxxx no. :P

Syntax of Function Pointer in C


Before we discuss syntax of function pointer. Lets just brush up some declaration in C.

int func(/* arg */);
This function will return an int data type when called.

int *func(/* arg */);
This function will return a pointer of type int.

int (*func)(/* arg */);
This will create a pointer to function returning data type int.


Hmmm confused?? Go through what i have written, couple of time and try to understand it.

Now lets see how it works in C...






#include <stdio.h>

void number(int a)
{
printf("You have entered number %d \n",a);
}

void square(int a)
{
printf("Square of entered number %d \n",a*a);
}


void main()
{
void (*func)(int a);
func = &number;
func(2);
// Givin '&' with function name is not necessary. But you should give that as good programing practice.
func = square;
func(2);
}


******************OUTPUT***********************


You have entered number 2
Square of entered number 4

***************************************************


Why should I use function pointer?

Ok. It sounds cool. But is it really helpful? Well of course yes. So where are Function Pointer used? Most common example i could find on net is qsort(). We will discuss that briefly. You can use function pointer when you dont know what the function will do? Let us understand with a simple example of qsort. 

Qsort is a Linux function. Used for quick sorting an array. It is declared as..


void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );



Here look at the declaration of function pointer  to  comparator. User  has to write that function. Ex.

#include <stdio.h>
#include <stdlib.h>

int values[] = { 13, 3, 100, 55, 76, 11 };

int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

void main ()
{
  int n;
  qsort (values, 6, sizeof(int), compare);
  for (n=0; n<6; n++)
     printf ("%d ",values[n]);
}

/* reference: http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/  */



******************OUTPUT***********************

3 11 13 55 76 100 

***************************************************

In a nutshell person who has made qsort function has implemented a function which knows how to implement quick sort on given values (in example value pointer), size of that data type (here; size of int) and how much value to compare (6 in our case). But he don't know how to compare and which value to compare. So the person who is using qsort is expected to write down function for that. Cool, isn't it? Now think where you can use it. And make most of it in your project. 

Suggestion are always welcomed :)



Sites that I have found useful :)

http://publications.gbdirect.co.uk/c_book/chapter5/function_pointers.html
This site provides a good understanding on syntax of function pointer in C.

http://www.cprogramming.com/tutorial/function-pointers.html
Hmm.. Lil' bit technical but still very very useful.

http://www.newty.de/fpt/intro.html
In great detail. Good one.


1 comment: