Home  >  Article  >  Backend Development  >  How to pass entire array as parameter to function in C language?

How to pass entire array as parameter to function in C language?

WBOY
WBOYforward
2023-09-09 17:37:021920browse

How to pass entire array as parameter to function in C language?

Array

An array is a group of related items with the same name. Here are two ways to pass an array as an argument to a function:

  • Passing an entire array as an argument to a function
  • Passing a single element as an argument to a function

Passing an entire array as a parameter to a function

  • To pass an entire array as a parameter, simply 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 understand about More information on passing an entire array as a parameter to a function -

#include<stdio.h>
main (){
   void number(int a[5]);
   int a[5], i;
   printf ("enter 5 elements</p><p>");
   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</p><p>");
   for (i=0; i<5; i++)
      printf("%d</p><p>" , a[i]);
}

Output

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

The above is the detailed content of How to pass entire array as parameter to function in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete