Passing Array as Parameter to Function in C

This “Passing Array as Parameter to Function in C ” explains how to pass base address of array as a parameter to function using example codes.


 

Pass Array as a parameter to a function :

  • It is not possible to pass all the elements of array to a function.
  • Passing array as a parameter means, passing only base address of array.
  • By using base address, if we perform any operations on index elements in the “called function” directly affects in the locations of “calling function”.

 

#include<stdio.h>
void main()
{
int a[5]={10,20,30,40,50},i;
modify(a,5);
printf(“elements are\n”);
for(i=0 ; i<5 ; i++)
{
printf(“%d\n”,a[i]);
}
}

void modify(int x[], int n)
{
int i;
for(i=0 ; i<n ; i++)
{
x[i]=x[i]+100;
}
}

Share this