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

How to send entire array as parameter in C language?

王林
Release: 2023-08-27 18:49:05
forward
1426 people have browsed it

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];
Copy after login

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};
Copy after login

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]);
}
Copy after login

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
Copy after login

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]);
   }
}
Copy after login

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
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template