Home > Backend Development > C++ > body text

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

WBOY
Release: 2023-09-09 17:37:02
forward
1883 people have browsed it

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

Output

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

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

Output

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

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!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!