How to pass entire array as an argument to a function in C language?


Array

The array is a group of related items that store with a common name. Following are the two ways of passing arrays as arguments to functions −

  • sending entire array as argument to function
  • sending individual elements as argument to function

Sending entire array as an argument to a function

  • To send entire array as argument, just send the array name in the function call.

  • To receive an array, it must be declared in the function header.

Example 1

#include<stdio.h>
main (){
   void display (int a[5]);
   int a[5], i;
   clrscr();
   printf ("enter 5 elements");
   for (i=0; i<5; i++)
      scanf("%d", &a[i]);
   display (a); //calling array
   getch( );
}
void display (int a[5]){
   int i;
   printf ("elements of the array are");
   for (i=0; i<5; i++)
      printf("%d ", a[i]);
}

Output

Enter 5 elements
10 20 30 40 50
Elements of the array are
10 20 30 40 50

Example 2

Let us consider another example to know more about passing entire array as argument to function −

#include<stdio.h>
main (){
   void number(int a[5]);
   int a[5], i;
   printf ("enter 5 elements
");    for (i=0; i<5; i++)       scanf("%d", &a[i]);    number(a); //calling array    getch( ); } void number(int a[5]){    int i;    printf ("elements of the array are
");    for (i=0; i<5; i++)       printf("%d
" , a[i]); }

Output

enter 5 elements
100
200
300
400
500
elements of the array are
100
200
300
400
500

Updated on: 09-Mar-2021

830 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements