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 78 79 80 81 82 83 84 85
|
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
typedef struct monElement
{
char nom[20],
prenom[20];
int age;
} ;
monElement * ajout_Einstein( monElement *Ancien, int position, int taille)
{
int NombreEntrees;
int le = sizeof(monElement);
int i;
// NombreEntrees = ??? // Ici je ne sais pas comment faire
struct monElement *Nouveau ;
Nouveau = ( struct monElement * ) malloc(sizeof(monElement)*(taille+1));
printf("Taille de Ancien = %d\n", taille);
printf("Le nom du premier elements est = %s : \n", Ancien[0].nom);
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 ;
Nouveau = ajout_Einstein(&Ancien[0], 2, sizeof(Ancien)/sizeof(monElement)); // ajout après la seconde entrée
printf("Le nom du troisieme element est = %s : \n", Nouveau[2].nom);
printf("Taille de Nouveau = %d\n", sizeof(Nouveau)/sizeof(monElement));
Ancien = Nouveau;
} |
Partager