NIELIT O Level January-16
Q 7 (a) . How does passing an array as an argument to a function differ from call by value?
Solution :
An array is a collection of similar data type which occupies continuous location on memory. Name of array is constant pointer which point (store address of first element of array) first element. When we passed an array to a function as an argument then we pass address of first element and if any change made inside of function to passing array then its effect original location because it is call by reference.
So when an array is passed to a function as an argument then it’s always being pass by reference.
As in figure array is passed to function as argument when array name ar is passed then address of first element which is 10 is passed. So any change made by function will affect original data. Following code is example to demonstrate this fact. All element of passing array will be increase by 10.
/* ################################
Girfa Student Help
Passing array to function
for more visit :
http://girfahelp.blogspot.in/p/c-language.html
################################
*/
#include<stdio.h>
#include<conio.h>
int
updatear(int *);
void main()
{
int ar[5],i;
clrscr();
for(i=0;i<5;i++)
{
printf("Enter
Number>> ");
scanf("%d",&ar[i]);
}
printf("\nArray data before
function call\n");
for(i=0;i<5;i++)
printf("\t%d",ar[i]);
updatear(ar);
printf("\nArray data after
after function call \n");
for(i=0;i<5;i++)
printf("\t%d",ar[i]);
getch();
}
int
updatear(int *ar)
{
int i;
for(i=0;i<5;i++)
ar[i]=ar[i]+10;
}
No comments:
Post a Comment