Functions and Function Pointers C 2017 ClassNote
Functions and Function Pointers C 2017 ClassNote
#include <stdio.h>
#include <stdlib.h>
int add(int,int); Function Prototype
int main(void) a 2
{
int a,b,r; b 3
printf("Enter a and b:");
scanf("%d%d",&a,&b); r 5
r=add(a,b); Function Calling
printf("Result=%d",r); x 2
return 0;
y 3
}
int add(int x,int y)
{ Function Definition
return x+y;
}
1
Govt. College of Engineering. & Ceramic Technology
Class Notes
Pranay Adak [email protected]
2
Govt. College of Engineering. & Ceramic Technology
Class Notes
Pranay Adak [email protected]
(*fact)=(*fact)*i;
}
return fact;
}
#include <stdio.h>
#include <stdlib.h>
int **create2dArray(int,int);
void init2dArray(int **,int,int);
void display2dArray(int **,int,int);
int main(void)
{
int row;
int col;
int **a;
printf("Enter the no of rows and columns respectively:");
scanf("%d%d",&row,&col);
a=create2dArray(row,col);
init2dArray(a,row,col);
display2dArray(a,row,col);
return 0;
}
int **create2dArray(int r,int c)
{
int i;
int **p;
p=(int **)calloc(r,sizeof(int *));
if(p!=NULL)
{
for(i=0;i<r;i++)
{
*(p+i)=(int *)calloc(c,sizeof(int));
}
}
else
{
printf("Error!!!\n");
exit(1);
}
return p;
}
void init2dArray(int **p,int r,int c)
3
Govt. College of Engineering. & Ceramic Technology
Class Notes
Pranay Adak [email protected]
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("Enter a[%d][%d]=",i,j);
scanf("%d",*(p+i)+j);
}
}
}
void display2dArray(int **p,int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d ",*(*(p+i)+j));
}
printf("\n");
}
}
Function pointers
Declaring a function pointer, -
<FunctionReturnType> (*<FunctionPointerName/id>)(<arguments>);
Example:
For the function prototype “int add(int,int);” the
declaration is,- int (*fnp)(int,int);
<FunctionPointerName/id>=&<FunctionName>;
4
Govt. College of Engineering. & Ceramic Technology
Class Notes
Pranay Adak [email protected]
Example:
fnp=&add;
(*<FunctionPointerName/id>)(<arguments>);
Example:
r=(*fnp)(a,b);
#include <stdio.h>
#include <stdlib.h>
int *factorial(int);
int main(void)
{
int n;
int *f;
int *(*fnp)(int);
printf("Enter the number:");
scanf("%d",&n);
fnp=&factorial;
f=(*fnp)(n);
printf("Factorial of %d is =%d",n,*f);
5
Govt. College of Engineering. & Ceramic Technology
Class Notes
Pranay Adak [email protected]
return 0;
}
int *factorial(int n)
{
int i;
int *fact;
fact=(int *)malloc(sizeof(int));
*fact=1;
for(i=1;i<=n;i++)
{
(*fact)=(*fact)*i;
}
return fact;
}
6
Govt. College of Engineering. & Ceramic Technology
Class Notes
Pranay Adak [email protected]
int *p;
p=(int *)malloc(x*sizeof(int));
if(p==NULL)
{
printf("Error!!!");
}
return p;
}
void init(int *p,int x)
{
int i;
for(i=0;i<x;i++)
{
printf("Enter a[%d]=",i);
scanf("%d",p+i);
}
}
void display(int *p,int x)
{
int i;
for(i=0;i<x;i++)
{
printf("%d ",*(p+i));
}
}
7
Govt. College of Engineering. & Ceramic Technology
Class Notes
Pranay Adak [email protected]
<FunctionReturnType> (*<FunctionPointerName/id[size]>)(<arguments>);
Example:
For the function prototypes “int add(int,int); and int sub(int,int);”
the declaration and assigning the values is,-
int (*fnp[])(int,int)={&add,&sub};
Calling a function using function pointer array, -
(*<FunctionPointerName/id>[i])(<arguments>);
Example:
r=(*fnp[i])(a,b);
8
Govt. College of Engineering. & Ceramic Technology
Class Notes
Pranay Adak [email protected]
p=(int *)malloc(x*sizeof(int));
if(p==NULL)
{
printf("Error!!!");
}
return p;
}
void init(int *p,int x)
{
int i;
for(i=0;i<x;i++)
{
printf("Enter a[%d]=",i);
scanf("%d",p+i);
}
}
void display(int *p,int x)
{
int i;
for(i=0;i<x;i++)
{
printf("%d ",*(p+i));
}
}