Passage d'un tableau de structure
Bonjour,
Je suis relativement novice en C (je code habituellement en Java) et je me torture l'esprit pour savoir :
1) comment passer un tableau de structure à une fonction.
2) trouver le nombre d'entrées dans la fonction sans être obligé de le passer en paramètre. J'ai essayé avec
- sizeof(tableau) ou même
- sizeof(tableau)/size(entrée)
mais sizeof(tableau) renvoie toujours 0.
3) retourner un nouveau tableau de structures pour remplacer l'ancien.
Voici un test (qui n'a pas de d’intérêt réel) et qui bien sûr donne des erreurs de compilation. Merci d'avance à tous ceux qui voudront bien m'aider
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
#include <stdlib.h>
#include <math.h>
#include <string.h>
typedef struct monElement
{
char nom[20],
prenom[20];
int age;
} ;
monElement * ajout_Einstein( monElement *Ancien, int position)
{
int NombreEntrees;
int le = sizeof(monElement);
int i;
// NombreEntrees = ??? // Ici je ne sais pas comment faire
struct monElement Nouveau[NombreEntrees+1]; // nouveau tableau
struct monElement ajout;
strcpy(ajout.nom,"Einstein");
strcpy(ajout.prenom,"Albert");
ajout.age = 28;
for(i=0; i<=position ; i++)
{
Nouveau[i] = Ancien[i];
}
Nouveau[position] = ajout; // On insère le nouvel élément
for (i=position; i < NombreEntrees; i++)
Nouveau[i+1] = Ancien[i];
return Nouveau;
}
int main()
{
struct monElement Ancien[5]; // on définit 5 entrées dans le tableau
struct monElement x0; // first structure object
strcpy(x0.nom,"Durand");
strcpy(x0.prenom,"Pierre");
x0.age = 42;
Ancien[0] = x0;
struct monElement x1;
strcpy(x1.nom,"Martin");
strcpy(x1.prenom,"Jean");
x1.age = 17;
Ancien[1] = x1;
struct monElement x2;
strcpy(x2.nom,"Poquelin");
strcpy(x2.prenom,"Jean-Baptistet");
x2.age = 62;
Ancien[2] = x2;
struct monElement x3;
strcpy(x3.nom,"Camus");
strcpy(x3.prenom,"Albert");
x3.age = 38;
Ancien[3] = x3;
struct monElement x4;
strcpy(x4.nom,"Dupont");
strcpy(x4.prenom,"Robert");
x4.age = 20;
Ancien[4] = x4;
struct monElement Nouveau[6];
Nouveau = ajout_Einstein(&Ancien, 2); // ajout après la seconde entrée
} |