Sep 13, 2014

C Programming #46: Pointer to function

Following article discusses function pointers. Till now we have discussed different types of data pointers.

Definition of pointers till now :

Pointers are special type of variable that store address of other variables.

Not just variables are stored in memory (either heap, stack) functions are also stored in memory (text section). Hence like how variables have address even functions have address associated with them. Address of a function can be accessed just like how the address of a variable was accessed.



E.g If the prototype of a function is

int func(int a);

Then address of where func is stored can be accessed as below
&func

As we had restriction in case of data pointers like, only integer pointer can store address of integer variable. Similar restriction also apply here for function pointer also, and both the return type and argument type should match with prototype.

Let me explain with an example.

If a funtion prototype is:
int fun(int a);
Then declaration of function pointer that could point to fun would be.
int (*pfun) (int);

"pfun" is a function pointer that points to function that takes one integer argument and returns an integer. Why are we putting brackets around pfun ? Say we had not put brackets what would be the declaration?
int *pfun (int);
In above case compiler would have interpreted pfun as function that take one integer as argument and returns an integer pointer. Compiler would have associated * with the return type. Hence brackets are used to make the compiler associate * with the name, so that it interprets it as function pointer. Now we have see how address of function is accessible, how a function pointer is accessible. lets see at practical example.

Write a C program to print "hello world, welcome to wonderful world of C" in different languages (english, spanish, german, french)

Using google translate I have noted down in all four languages.

english - hello world, welcome to wonderful world of C
spanish - hola mundo , bienvenida a maravilloso mundo de la C
german - Hallo Welt , willkommen in der wunderbaren Welt C
french - Bonjour tout le monde , bienvenue à monde merveilleux de la C

Say we write 4 function, hello_english, hello_spanish, hello_german, hello_french that would print these string.

We can write then write a pointer to any one of these function to print Hello in respective language.


#include <stdio.h>

void hello_english(void);
void hello_spanish(void);
void hello_german(void);
void hello_french(void);

int main()
{
   void (*hello) (void);

   hello = &hello_english;
   hello();

   hello = &hello_spanish;
   hello();

   hello = &hello_german;
   hello();

   hello = &hello_french;
   hello();

}
void hello_english(void)
{
   printf("hello world, welcome to wonderful world of C\n");
   return;
}
void hello_spanish(void)
{
   printf("hola mundo , bienvenida a maravilloso mundo de la C\n");
   return;
}
void hello_german(void)
{
   printf("Hallo Welt , willkommen in der wunderbaren Welt C\n");
   return;
}
void hello_french(void)
{
   printf("Bonjour tout le monde , bienvenue à monde merveilleux de la C\n");
   return;
}

Output of the above program is:

hello world, welcome to wonderful world of C
hola mundo , bienvenida a maravilloso mundo de la C
Hallo Welt , willkommen in der wunderbaren Welt C
Bonjour tout le monde , bienvenue à monde merveilleux de la C
Observation.
  1. hello function pointer abstracted four function with respect to language.
  2. function pointers are used for power abstraction.
  3. During run time we could decide which function is called.
  4. There are extensively used in callback functions.

Links

No comments :

Post a Comment