Array is a collection of similar
data type which occupies continuous location on memory. Array’s name is a
constant pointer which point first element of array. So when an array is passing
to a function. Address of first element pass to function instead of value. If any
change made inside of function body with array, then it affects actual value of
passed array. Following program demonstrate this fact.
#include<stdio.h>
int update(int[]);
void main()
{
int arr[5],i;
for(i=0;i<5;i++)
arr[i]=i+1;
printf("\nBefore function call\
Array Data\n");
for(i=0;i<5;i++)
printf("\t%d",arr[i]);
printf("\nAfter function call\
Array Data\n");
update(arr);
for(i=0;i<5;i++)
printf("\t%d",arr[i]);
}
int update(int ar[])
{
int i;
for(i=0;i<5;i++)
ar[i]+=5;
}
In given program array is initialed
with 1 to 5 respectively. For loop is
used to print array value before and after function call you will notice in
output that every value of array is incremented by 5 after function call. This
is done inside of update function body that effect array’s actual data. So
given example is a demo for sake of that array is passing by reference to a
function.
No comments:
Post a Comment