Pointers as
function Arguments:
Arguments
can be passed to a function in 2 ways.
(i)
call by value and
(ii)
call by reference
v
In call by value, we pass values of actual
arguments to the corresponding formal arguments. The changes in formal
arguments do not reflect in actual arguments.
v
In call by reference, addresses of actual
arguments are passed from calling function to the called function. The formal
arguments that receive addresses are declared as pointers variable of same datatype.
Here, the changes that are made in formal arguments are reflected in the actual
arguments also.
A Program to
find the bigger of two numbers using pointers as function arguments.
#include<stdio.h>
#include<conio.h>
int large(int *, int *);
void main()
{
int
x,y,z;
printf(“\n
enter any 2 values”);
scanf(“%d%d”,&x,&y);
z=large(&x,&y);
printf(“biggest=%d”,z);
}
int large(int *p, int *q)
{
if(*p>*q)
return
(*p)
else
return
(*q);
}
A
Program to print the array elements using pointers as function arguments.
#include<stdio.h>
#include<conio.h>
void array(int *);
void main()
{
int a[5]={1,2,3,4,5};
clrscr();
array(a);
}
void array(int *x)
{
int i;
for(i=0;i<5;i++)
{
printf(“%d”,*x);
x++;
}
}
No comments:
Post a Comment