Passing array as parameter in C

Previous
Next

Passing array as parameter to function:

  • Array variable stores base address of memory block.
  • Passing array is nothing but, passing the address of array to the function.
  • Duplicate array will not be created in the called function.
#include<stdio.h>
void display(int[]);
int main()
{
	int a[5] = {10,20,30,40,50};
	display(a);
	return 0;	
}
void display(int x[])
{
	int i;
	printf("Array elements are : \n");
	for(i=0 ; i<5 ; i++)
	{
		printf("%d \n", x[i]);
	}
}
#include<stdio.h>
void modify(int[]);
int main()
{
	int a[5] = {10,20,30,40,50}, i;
	modify(a);
	printf("Array elements are : \n");
	for(i=0 ; i<5 ; i++)
	{
		printf("%d \n", a[i]);
	}
	return 0;	
}
void modify(int x[])
{
	int i;
	for(i=0 ; i<5 ; i++)
	{
		x[i]=x[i]-5;
	}
}
Previous
Next

Add Comment

Courses Enquiry Form