Passing Arrays as Function Arguments in C

Passing Arrays as Function Arguments in C

#include<stdio.h>

void disp(int [],int);      //declaration of function. int [] to pass array as functon argument and int to pass array size

main()
{

int array[5];       //intialize array
int i;

printf("Enter array elements:\n");        //print message on screen to enter elements

for(i=0;i<5;i++)
{

scanf("%d",&array[i]);    //take input from user

}

printf("\n");

for(i=0;i<5;i++)
{

printf("Elements Are:%d\n",array[i]);    //print elements which user entered

}

disp(array,5);        //pass array as function argument to disp(int [],int). you can also declare function as disp(int a[],int)


}

void disp(int ar[],int j)    //function definition
{

 int i,sum=0;

 for(i=0;i<j;i++)
 {

   sum=sum+ar[i]; //sum array elements

 }

printf("\n");

printf("%d",sum); //print sum of array elements

printf("\n\n");

}


Related Posts