14.20 – Pointer to a function

by subbu on February 4, 2014

Pointer to a function:

Like a variable or an array even a function is stored some where in the memory, has its address. The name of the function gives the address of a function.

 
#include<stdio.h>
void display()
{
  printf("Hello World");
}
int main()
{
  printf("%u",display);
  return 0;
}

Output:
657

As the name of function gives the address, a pointer can be created to the function. Where the type of pointer must be of function type (return type) and the list of argument types must be specified with the declaration. Say for example

pointer to the function

int sum(int x,int y)
{
    return x+y;
}

is

 
int *p(int,int);

but, the compiler gives error as p(int,int) evaluates before identifying p as a pointer because () has higher priority than *
Hence we need to give explicit priority to *p by placing it within ().

 
int (*p)(int,int);  /* declaration of pointer */
p=sum;              /* assigning the address */

pointer to a function

We can call the function through the pointer using dereferencing.

 
s=(*p)(10,20);

Putting altogether

 
#include<stdio.h>
int sum(int x,int y)
{
   return x+y;
}
int main()
{
  int s;
  int (*p)(int,int);      /* declaring pointer to function */
  p=sum;                  /* assigning address of a function to the pointer */
  s=(*p)(30,40);       /* accessing function through dereferencing */ 
  printf("Sum of two numbers %d",s);
  return 0;
}

Output:
Sum of two numbers 70

Sending the address of a function as argument:

 
#include<stdio.h>
void sayfox()
{
   printf("CodingFox");
}
void sayworld()
{
   printf("\nHello World");
}
void display(void (*p)(),void (*q)())
{
   (*p)();
   (*q)();
}
int main()
{
  display(sayfox,sayworld); 
  return 0;
}

Output:
CodingFox
Hello World

sending the address of function

Sending the addresses of sayfox() and sayworld() to display()
The display() function calls both the functions using dereferencing

Specification: Accept two integers and print the sum, subtraction using two functions getsum(), getsub() and sending these into other function display() as arguments

#include<stdio.h>
int getsum(int x,int y)
{
  return x+y;
}
int getsub(int x,int y)
{
  return x-y;
}
void manip(int (*p)(int,int),int (*q)(int,int))
{
   int a,b,c,d;
   printf("Enter two numbers:\n");
   scanf("%d%d",&a,&b);
   c=(*p)(a,b);
   d=(*q)(a,b);
   printf("Sum %d",c);
   printf("\nSubtaction %d",d);
}
int main()
{
  manip(getsum,getsub);
  return 0;
}

Output:
Enter two numbers:
12
6
Sum 18
Subtraction 6

Addresses of  getsum() and getsub() are send to manip().
Both p, q are pointers to getsum() and getsub().
Called the functions by p, q using dereferencing

Pointer array to functions:

Array of pointers can be defined to store the addresses of different functions. So that, functions can be called in a sequence.

 
#include<stdio.h>
void one()
{
   printf("Keep it ");
}
void two()
{
  printf("small ");
}
void three()
{
   printf("and simple");
}
int main()
{
  void (*p[3])();
  int i;
  p[0]=one;
  p[1]=two;
  p[2]=three;
  /*
  for(i=0;i<3;i++)
    (*(p[i]))();  */
  for(i=0;i<3;i++)
    (*(*(p+i)))();
  return 0;
}

Output:
Keep it small and simple
p is a pointer array refers three functions.
Through dereferencing p could access all the three functions

Previous post:

Next post: