1
M R S . D I P A L I M E H E R
M O D E R N C O L L E G E O F A R T S , S C I E N C E A N D C O M M E R C E ,
G A N E S H K H I N D , P U N E 4 1 1 0 1 6
POINTER TO FUNCTION/
FUNCTION POINTER
( IN C PROGRAMMING)
POINTER
2
 Pointer is a variable which stores
address of another variable. It is denoted
by * operator.
 Functions are always stored somewhere
in memory. So they also have address.
So we can also define pointer to function
/ function pointer which will store address
of function.
STEPS TO DECLARE FUNCTION POINTER
Declare the function pointer- to declare function pointer by any name
with precedence of * operator. This pointer is written into opening
closing rounded bracket. Return data type if any will be written first
then pointer with rounded brackets then data type of actual
parameters will be written in rounded brackets. Declaring function
pointer does not means that function can be directly called.
Store the address- to call the function address of function must be
stored in function pointer
Call the function using function pointer- instead of using function
name to call it now use function pointer name to call the function (if
arguments are there then pass it i.e. actual parameters)
3
DECLARING A FUNCTION POINTER
Function
return type name_of_the function( data type arguement1, data type argument
2,…);
return type (* pointer name)( data type argument 1, data type argument2,..);
For example
4
Return
type
Name of the
function
Argument (Actual
Parameters)
Explanation
void add (int , int ); Add is such a function which
accepts two integers and prints
addition of them
void (*ptr) (int, int); ptr is a pointer to such function
which accepts two integers.( the
function can do any thing. In this
case it will do addition)
EXAMPLES..
add is a function
which accepts
nothing and prints
addition of them
void add(void); void (*ptr) (void); ptr is pointer to
such function
which does not
accepts anything
add is a function
which accepts two
integers and prints
addition of them
void add(int, int); void (*ptr) ( int,int); ptr is pointer to
such function
which accepts two
intergers
add is a function
which accepts two
integers and
returns addition of
them
int add(int, int); int (*ptr) ( int,int); ptr is pointer to
such function
which accepts two
integers and
returns one integer
5
HOW TO CALL FUNCTION USING FUNCTION POINTER
return type name_of_the function( data type arguement1, data
type argument 2,…)
void add(int , int);// declaring function in main function
int a,b;
add ( a, b)//calling function
return type (* pointer name)( data type argument 1, data type
argument2,..)
void(*ptr) (int, int);// declaring function pointer in main function
int a,b;
ptr=add; // storing address of function pointer
ptr( a, b);// calling function using function pointer
6
EXAMPLE
( ON LINUX PLATFORM WRITING FUNCTION DEFINITION
BEFORE MAIN)
steps Normal Programme
using Function
Programme using
function pointer
steps
Declare a
function
void add( int a, int b)
{
printf(“nAddition=%d”,a+
b);
}
void add( int a, int b)
{
printf(“nAddition=%d”,a+b);
}
In main
call the
function
int main()
{int x,y;
printf(“enter two
values:”);
scanf(“%d%d”,&x,&y);
add(x,y);
}
int main()
{
void (*p)(int, int);
int x,y;
printf(“enter two values:”);
scanf(“%d%d”,&x,&y);
ptr=add;
ptr(x,y);
}
Declare the
function pointer
Store the address
Call the function
using function
pointer
7
EXAMPLE
( ON LINUX PLATFORM WRITING FUNCTION DEFINITION
AFTER MAIN)
steps Normal Programme
using Function
Programme using
function pointer
steps
In main
call the
function
int main()
{int x,y;
void add(int, int);
printf(“enter two
values:”);
scanf(“%d%d”,&x,&y);
add(x,y);
}
int main()
{
void (*p)(int, int);
int x,y;
void add( int,int);
printf(“enter two values:”);
scanf(“%d%d”,&x,&y);
ptr=add;
ptr(x,y);
}
Declare the
function pointer
Store the address
Call the function
using function
pointer
Declare a
function
after
main
void add( int a, int b)
{
printf(“nAddition=%d”,a+
b);
}
void add( int a, int b)
{
printf(“nAddition=%d”,a+b);
}
8
CALLING FUNCTION IN MAIN
Without function pointer With function pointer
#include<stdio.h> #include<stdio.h>
void add()
{
int a,b;
printf(“n Enter two values:”);
scanf(“%d%d”, &a,&b);
printf(“naddition=%d”,a+b);
}
void add()
{
int a,b;
printf(“n Enter two values:”);
scanf(“%d%d”, &a,&b);
printf(“naddition=%d”,a+b);
}
int main()
{
add();// directly calling the function
}
int main()
{ void(*ptr) (void);// declaring function
pointer
ptr=add;// pointer storing address of
function
ptr();//calling of function written before main
}
9
CALLING FUNCTION IN MAIN WITH ARGUMENTS
Without function pointer With function pointer
#include<stdio.h> #include<stdio.h>
void add(int a,int b)
{
printf(“naddition=%d”,a+b);
}
void add(int a,int b)
{
printf(“naddition=%d”,a+b);
}
int main()
{
int x,y;
printf(“nEnter two numbers:”);
scanf(“%d%d”, &x,&y);
add(x,y);// calling the function with arguments
}
int main()
{ void(*ptr) (int,int);// declaring function
pointer
int x,y;
printf(“nEnter two numbers:”);
scanf(“%d%d”,&x,&y);
ptr=add;// pointer storing address of function
ptr(x,y);//calling of function using function
//pointer and function written before main
}
10
CALLING FUNCTION IN MAIN WITH ARGUMENTS
AND RETURNING VALUE
Without function pointer With function pointer
#include<stdio.h> #include<stdio.h>
void add(int a, int b)
{
return a+b;
}
void add(int a, int b)
{
return a+b;
}
int main()
{
int x,y,z;
printf(“nEnter two numbers:”);
scanf(“%d%d”,&x,&y);
z=add(x,y);// calling the function with arguments
printf(“n addition=%d”, z);
}
int main()
{ int (*ptr) (int, int);// declaring function pointer
int x,y,z;
printf(“nEnter two numbers:”);
scanf(“%d%d”, &x,&y);
ptr=add;// pointer storing address of function
z=ptr(x,y); //calling of function using
function pointer and function
written before main
printf(“n addition=%d”, z);
}
11
CALLING FUNCTION IN MAIN WITH ARGUMENTS AND RETURNING VALUE
Without function pointer With function pointer
#include<stdio.h> #include<stdio.h>
int add(int x)
{ int sum=0;
for(i=1;i<=x;i++)
{
sum=sum+i;
}
return sum;
}
int add(int x)
{ int sum=0;
for(i=1;i<=x;i++)
{
sum=sum+i;
}
return sum;
}
int main()
{
int n,z=0;
printf(“nEnter n:”);
scanf(“%d”, &n);
z=add(n);
printf(“n sum of first n
numbers=%d“,z);
}
int main()
{ int (*ptr) (int);// declaring function pointer
int n,z=0;
printf(“nEnter n:”);
scanf(“%d”, &n);
ptr=add;// pointer storing address of function
z=ptr(n); //calling of function using
function pointer and function
written before main
printf(“nsum of first n numbers=%d”, z); 12
13

Function Pointer

  • 1.
    1 M R S. D I P A L I M E H E R M O D E R N C O L L E G E O F A R T S , S C I E N C E A N D C O M M E R C E , G A N E S H K H I N D , P U N E 4 1 1 0 1 6 POINTER TO FUNCTION/ FUNCTION POINTER ( IN C PROGRAMMING)
  • 2.
    POINTER 2  Pointer isa variable which stores address of another variable. It is denoted by * operator.  Functions are always stored somewhere in memory. So they also have address. So we can also define pointer to function / function pointer which will store address of function.
  • 3.
    STEPS TO DECLAREFUNCTION POINTER Declare the function pointer- to declare function pointer by any name with precedence of * operator. This pointer is written into opening closing rounded bracket. Return data type if any will be written first then pointer with rounded brackets then data type of actual parameters will be written in rounded brackets. Declaring function pointer does not means that function can be directly called. Store the address- to call the function address of function must be stored in function pointer Call the function using function pointer- instead of using function name to call it now use function pointer name to call the function (if arguments are there then pass it i.e. actual parameters) 3
  • 4.
    DECLARING A FUNCTIONPOINTER Function return type name_of_the function( data type arguement1, data type argument 2,…); return type (* pointer name)( data type argument 1, data type argument2,..); For example 4 Return type Name of the function Argument (Actual Parameters) Explanation void add (int , int ); Add is such a function which accepts two integers and prints addition of them void (*ptr) (int, int); ptr is a pointer to such function which accepts two integers.( the function can do any thing. In this case it will do addition)
  • 5.
    EXAMPLES.. add is afunction which accepts nothing and prints addition of them void add(void); void (*ptr) (void); ptr is pointer to such function which does not accepts anything add is a function which accepts two integers and prints addition of them void add(int, int); void (*ptr) ( int,int); ptr is pointer to such function which accepts two intergers add is a function which accepts two integers and returns addition of them int add(int, int); int (*ptr) ( int,int); ptr is pointer to such function which accepts two integers and returns one integer 5
  • 6.
    HOW TO CALLFUNCTION USING FUNCTION POINTER return type name_of_the function( data type arguement1, data type argument 2,…) void add(int , int);// declaring function in main function int a,b; add ( a, b)//calling function return type (* pointer name)( data type argument 1, data type argument2,..) void(*ptr) (int, int);// declaring function pointer in main function int a,b; ptr=add; // storing address of function pointer ptr( a, b);// calling function using function pointer 6
  • 7.
    EXAMPLE ( ON LINUXPLATFORM WRITING FUNCTION DEFINITION BEFORE MAIN) steps Normal Programme using Function Programme using function pointer steps Declare a function void add( int a, int b) { printf(“nAddition=%d”,a+ b); } void add( int a, int b) { printf(“nAddition=%d”,a+b); } In main call the function int main() {int x,y; printf(“enter two values:”); scanf(“%d%d”,&x,&y); add(x,y); } int main() { void (*p)(int, int); int x,y; printf(“enter two values:”); scanf(“%d%d”,&x,&y); ptr=add; ptr(x,y); } Declare the function pointer Store the address Call the function using function pointer 7
  • 8.
    EXAMPLE ( ON LINUXPLATFORM WRITING FUNCTION DEFINITION AFTER MAIN) steps Normal Programme using Function Programme using function pointer steps In main call the function int main() {int x,y; void add(int, int); printf(“enter two values:”); scanf(“%d%d”,&x,&y); add(x,y); } int main() { void (*p)(int, int); int x,y; void add( int,int); printf(“enter two values:”); scanf(“%d%d”,&x,&y); ptr=add; ptr(x,y); } Declare the function pointer Store the address Call the function using function pointer Declare a function after main void add( int a, int b) { printf(“nAddition=%d”,a+ b); } void add( int a, int b) { printf(“nAddition=%d”,a+b); } 8
  • 9.
    CALLING FUNCTION INMAIN Without function pointer With function pointer #include<stdio.h> #include<stdio.h> void add() { int a,b; printf(“n Enter two values:”); scanf(“%d%d”, &a,&b); printf(“naddition=%d”,a+b); } void add() { int a,b; printf(“n Enter two values:”); scanf(“%d%d”, &a,&b); printf(“naddition=%d”,a+b); } int main() { add();// directly calling the function } int main() { void(*ptr) (void);// declaring function pointer ptr=add;// pointer storing address of function ptr();//calling of function written before main } 9
  • 10.
    CALLING FUNCTION INMAIN WITH ARGUMENTS Without function pointer With function pointer #include<stdio.h> #include<stdio.h> void add(int a,int b) { printf(“naddition=%d”,a+b); } void add(int a,int b) { printf(“naddition=%d”,a+b); } int main() { int x,y; printf(“nEnter two numbers:”); scanf(“%d%d”, &x,&y); add(x,y);// calling the function with arguments } int main() { void(*ptr) (int,int);// declaring function pointer int x,y; printf(“nEnter two numbers:”); scanf(“%d%d”,&x,&y); ptr=add;// pointer storing address of function ptr(x,y);//calling of function using function //pointer and function written before main } 10
  • 11.
    CALLING FUNCTION INMAIN WITH ARGUMENTS AND RETURNING VALUE Without function pointer With function pointer #include<stdio.h> #include<stdio.h> void add(int a, int b) { return a+b; } void add(int a, int b) { return a+b; } int main() { int x,y,z; printf(“nEnter two numbers:”); scanf(“%d%d”,&x,&y); z=add(x,y);// calling the function with arguments printf(“n addition=%d”, z); } int main() { int (*ptr) (int, int);// declaring function pointer int x,y,z; printf(“nEnter two numbers:”); scanf(“%d%d”, &x,&y); ptr=add;// pointer storing address of function z=ptr(x,y); //calling of function using function pointer and function written before main printf(“n addition=%d”, z); } 11
  • 12.
    CALLING FUNCTION INMAIN WITH ARGUMENTS AND RETURNING VALUE Without function pointer With function pointer #include<stdio.h> #include<stdio.h> int add(int x) { int sum=0; for(i=1;i<=x;i++) { sum=sum+i; } return sum; } int add(int x) { int sum=0; for(i=1;i<=x;i++) { sum=sum+i; } return sum; } int main() { int n,z=0; printf(“nEnter n:”); scanf(“%d”, &n); z=add(n); printf(“n sum of first n numbers=%d“,z); } int main() { int (*ptr) (int);// declaring function pointer int n,z=0; printf(“nEnter n:”); scanf(“%d”, &n); ptr=add;// pointer storing address of function z=ptr(n); //calling of function using function pointer and function written before main printf(“nsum of first n numbers=%d”, z); 12
  • 13.