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

How to send entire array as parameter in C language?

王林
王林forward
2023-08-27 18:49:051413browse

How to send entire array as parameter in C language?

An array is a set of related items stored using a common name.

Declare an array

The syntax for declaring an array is as follows-

datatype array_name [size];

Initialization

The array can be initialized in two ways, as follows-

  • Initialized at compile time.
  • Runtime initialization.

Arrays can also be initialized at declaration time as shown below -

int a[5] = {100,200,300,400,500};

Function

A function is a self-contained block that performs a specific well-defined Task. The two methods of passing an array as a parameter to a function are as follows -

  • Send the entire array as a parameter to the function.

  • Send individual elements as parameters to the function.

Now, let us understand how to send the entire array as parameter to a function in C.

Sending the entire array as parameter to a function

  • To send the entire array as a parameter, try calling it in the function.

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

Example 1

Please refer to the example given below-

#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); // Sending entire array &lsquo;a&rsquo; using array name
   getch( );
}
void display (int a[5]) {//receiving entire array
   int i;
   printf ("elements of the array are");
   for (i=0; i<5; i++)
      printf("%d ", a[i]);
}

Output

When the above code together When compiled and executed, it produces the following result -

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

Example 2

The following is a C program to print the elements in an array in reverse order-

#include<stdio.h>
void main(){
   //Declaring the array - run time//
   int array[5],i;
   void rev(int array[5]);
   //Reading elements into the array//
   printf("Enter elements into the array: ");
   //For loop//
   for(i=0;i<5;i++){
      //Reading User I/p//
      printf("array[%d] :",i);
      scanf("%d",&array[i]);
   }
   //Displaying reverse order of elements in the array//
   printf("The elements from the array displayed in the reverse order are :");
   rev(array); // Sending entire array &lsquo;a&rsquo; using array name
   getch();
}
void rev(int array[5]){ //receiving entire array
   int i;
   for(i=4;i>=0;i--){
      //Displaying O/p//
      printf("array[%d] :",i);
      printf("%d",array[i]);
   }
}

Output

When the above programs are compiled together and executed, the following results are produced -

Enter elements into the array:
array[0] :23
array[1] :34
array[2] :12
array[3] :56
array[4] :12
The elements from the array displayed in the reverse order are:
array[4] :12
array[3] :56
array[2] :12
array[1] :34
array[0] :23

The above is the detailed content of How to send entire array as parameter 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