chris68fr
Messages postés2Date d'inscriptionmercredi 1 janvier 2003StatutMembreDernière intervention19 avril 2003
-
5 avril 2003 à 14:24
BruNews
Messages postés21041Date d'inscriptionjeudi 23 janvier 2003StatutModérateurDernière intervention21 août 2019
-
5 avril 2003 à 22:10
Comment fait-t-on pour remplir un tableau dans une fonction (tableau defini dans main)?
cs_vieuxLion
Messages postés455Date d'inscriptionsamedi 26 octobre 2002StatutMembreDernière intervention 6 avril 20048 5 avril 2003 à 22:03
la réponse, pas très explicite, ne contient pas de fonction.
Le principal problème est le PASSAGE du tableau à la fonction.
La solution la plus courante est la suivante où la fonction reçoit aussi la longueur du tableau :
#include
using namespace std;
void initTab(int tab [], int taille)
{
for (int i=0; i<taille; i++)
tab[i]=i;
}
void ecritTab(int tab [], int taille)
{
for (int i=0; i<taille; i++)
cout << tab[i]<< " ";
cout << endl;
}
int main()
{
int tableau[10];
initTab(tableau, sizeof(tableau)/sizeof(*tableau));
ecritTab(tableau, sizeof(tableau)/sizeof(*tableau));