0% found this document useful (0 votes)
2 views

Function Pointer

first m.sc , calicut university , function pointer

Uploaded by

LEESHMA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Function Pointer

first m.sc , calicut university , function pointer

Uploaded by

LEESHMA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Function Pointer

• As we knowthat wecan createa pointerof any


dataitnyt,pcehsaur,cfhloaast, we can also createa pointerpointingto
a function.
• Thecode of a function alwaysresides in memory, which means
that the fun someaddress. Wecan get the address of memory
by usingthe function po
#includest<dio.>
h intmain()
{
print(f"Address of main()
functiopn",imsa%i)n; return 0;
}
Declaration of a function
pointer
• Till now, wehaveseen that the functions have addresses, so
wecan create that can contain these addresses, and hencecan
point them.
Syntaxof functiopnointer
return typep(t*r_name
For example:
int(*ip) (int);
• Inthe abovedeclarat*iopnis, a pointerthat points toa function which
returns a intvalue and accepts an integervalue as an
argument.

• float (f*p) (float);


• In the abovedeclaratfipoins,a*pointerthat points to a function that
Tillnow, we have learnt howto declarethe function pointer.
Ournext ste assign the address of a function to the
function pointer.

float (f*p) (int,int); //Declaration of a


function pointer. floatfunc( int,int);
//Declaration of function.
fp=func; //Assigningaddrefsusnoctfo
thefppointer.
Callingafunctionthroughafunctionpointe
r• Wealreadkynowhowtocallafunctionintheusuawl

ay.Now,wewillseehow
tocallafunctiounsingafunctiopnointe.r
Supposweedeclaraefunctioansgivenbelow:
floatfunc(int,int); //
Declaratioonfafunctio.n
Callinganabovefunctiounsingausuawl
ayisgivenbelow:
Callinga function usinga function pointeris given
below:

result=(*fp)(a, b); // Callingafunctionusingfunctionpointe.r


Or
result= fp(a, b); // Callinga
functionusingfunctionpointera, nd
indirectionoperatocranberemove.d
Theeffecotfcallingafunctiobnyitsnameorfunctionpointeirsthesam.eIf
we
areusingthefunctiopnointerw,ecanomitheindirectioonperatoarswedi
#include
<stdio.h> int
add(int,int);
int main()
{
int a,b;
int (*ip)
(int,int); int
result;
printf("Enter the values of a and b :
"); scanf("%d %d",&a,&b);
ip=add;
result=(*ip)
(a,b);
printf("Value
after addition
is :
%d",result);
return 0;
}
int add(int a,int
b)
{
int
ArrayofFunctionPointers
Functiopnointerasreusedinthoseapplicatiownsherewedonotknowinadva
nce
whichfunctionwillbecalle.dInanarrayoffunctionpointersa,rraytakesth
e
addresseosfdifferenftunctionas,ndtheappropriaftuenctionwillbecalledbas
ed ontheindexnumbe.r
#include <stdio.h>
float
add(float,int);
float
sub(float,int);
float
mul(float,int);
float // variable
div(float,int); declaration.
int float
main()(*fp[4]) (float,int); // function pointer
{ declaration.
fp[0]=add; // assigning addresses to the elements of an array of a pointer
function
float fp[1]=sub; .
fp[2]=mul
x; int
;
y;
fp[3]=div
;
printf("Enter the values of x and
printf("\nSum
y of two
:"); scanf("%f values is : %f",r);
%d",&x,&y);
r=(*fp[1])
float (x,y);
r=(*fp[0]) (x,y);// Calling sub() function.
// Calling
printf("\nDifference
add() function. of two values is : %f",r);
r=(*fp[2]) (x,y); // Calliung sub()
function. printf("\nMultiplication of two values
is : %f",r); r=(*fp[3]) (x,y); // Calling div()
function. printf("\nDivision of two values is :
%f",r);
return 0;
float add(float y)
x,int
{
float a=x+y;
return a;
}
float sub(float y)
x,int
{
float a=x-y;
return a;
}
float mul(float y)
x,int
{
float a=x*y;
return a;
}
float div(float y)
x,int
{
float a=x/y;
return a;
Return pointer
fromfunctions
int * myFunction() {
.
.
.
}

• itis not a good idea to return the address of a local


variableoutside the funct wouldhave to define the local
variableas static variable.
#include
<stdio.h> /* main function to call above defined
#include function */ int main () {
<time.h>
/* a pointer to an int
/* function to generate and return */
random numbers. */ int *p;
int * getRandom( ) { int i;
p =
static int getRandom();
r[10]; int i; for ( i = 0; i < 10; i++ ) {
printf("*(p + [%d]) : %d\n", i, *(p +
/* set the seed i) );
*/ }
srand( (unsigned return
)time( NULL ) ); } 0;

for ( i = 0; i < 10; +


+i) { r[i] = rand();
printf("%d\n", r[i] );
}

return r;
1523198053
1187214107
1108300978
430494959
1421301276
930971084
123250484
106932140
1604461820
149169022
*(p + [0]) :
1523198053
*(p + [1]) :
1187214107
*(p + [2]) :
1108300978
*(p + [3]) :
430494959
*(p + [4]) :
1421301276
*(p + [5]) :
930971084

You might also like