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
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct spectacle {
char intitule[50];
int nbPlace;
}t_spectacle;
t_spectacle *tabSpectacles;
void remplir_un_element (t_spectacle *tabSpectacles, int position, const char* intitule, int nbPlace) {
memcpy (tabSpectacles[position].intitule, intitule, strlen (intitule));
tabSpectacles[position].nbPlace = nbPlace;
}
int main (int argc, char **argv) {
/* Allocation mémoire pour 3 éléments */
tabSpectacles = malloc (3 * sizeof (t_spectacle));
/* Remplissage des 3 éléments */
remplir_un_element (tabSpectacles, 0, "En attendant goto", 10);
remplir_un_element (tabSpectacles, 1, "Tant qu'il y aura des pommes", 4);
remplir_un_element (tabSpectacles, 2, "Notre pam de paris", 6);
for (int i=0; i < 3; i++)
printf ("%s : %d\n", tabSpectacles[i].intitule, tabSpectacles[i].nbPlace);
/* Libération mémoire */
free (tabSpectacles);
return 0;
} |
Partager