Maison  >  Article  >  développement back-end  >  Comment passer un tableau entier en paramètre pour fonctionner en langage C ?

Comment passer un tableau entier en paramètre pour fonctionner en langage C ?

WBOY
WBOYavant
2023-09-09 17:37:021846parcourir

Comment passer un tableau entier en paramètre pour fonctionner en langage C ?

Array

Un tableau est un groupe d'éléments liés portant le même nom. Voici deux manières de passer un tableau en argument à une fonction :

  • Passer l'intégralité du tableau en argument à la fonction
  • Passer un seul élément en argument à la fonction

Passer l'intégralité du tableau en argument à la fonction

  • Pour passer l'intégralité du tableau en paramètre, envoyez simplement le nom du tableau dans l'appel de la fonction.

  • Pour recevoir un tableau, il faut le déclarer dans l'entête de la fonction.

Exemple 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

Exemple 2

Considérons un autre exemple pour en savoir plus sur le passage du tableau entier comme argument à une fonction -

#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

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer