#include #include struct horaire { int heure; int minute; int seconde; }; void afficherHoraire(struct horaire h) { printf("%02d:%02d:%02d", h.heure, h.minute, h.seconde); } void afficherTableauHoraires(struct horaire* th, int taille) { for (int i = 0; i < taille; i++) { afficherHoraire(th[i]); printf("\n"); } } void afficherTableauPointeursHoraire(struct horaire** th, int taille) { for (int i = 0; i < taille; i++) { afficherHoraire(*th[i]); printf("\n"); } } struct horaire tirerAuSortHoraire(void) { struct horaire h = { rand() % 24,rand() % 60,rand() % 60 }; return h; } struct horaire* creerTableauHorairesTiresAuSort(int taille) { struct horaire* th = (struct horaire*) calloc(taille, sizeof(struct horaire)); if (th != NULL) { for (int i = 0; i < taille; i++) { th[i] = tirerAuSortHoraire(); } } return th; } struct horaire** creerTableauHoraires(int taille) { struct horaire** th = (struct horaire**) calloc(taille, sizeof(struct horaire*)); if (th != NULL) { int i = 0; while ((i < taille) && ((th[i] = (struct horaire*) calloc(1, sizeof(struct horaire))) != NULL)) { i++; } if (th[taille - 1] == NULL) { for (int j = 0; j < i; j++) { free(th[j]); th[j] = NULL; } } } return th; } void libererTableauHoraires(struct horaire** th, int taille) { if (th != NULL) { for (int i = 0; i < taille; i++) { free(th[i]); th[i] = NULL; } free(th); th = NULL; } } #define TAILLE_1 5 void tester1(void) { const int seed = 6; srand(seed); struct horaire th[TAILLE_1]; printf("%d %zu %zu\n", TAILLE_1, sizeof(struct horaire), sizeof(th)); for (int i = 0; i < TAILLE_1; i++) { th[i] = tirerAuSortHoraire(); } afficherTableauHoraires(th, TAILLE_1); printf("\n"); } #undef TAILLE_1 void tester2(int taille) { const int seed = 6; srand(seed); struct horaire* th = (struct horaire*) calloc(taille, sizeof(struct horaire)); printf("%d %zu %zu\n",taille, sizeof(struct horaire*), sizeof(th)); if (th != NULL) { afficherTableauHoraires(th, taille); printf("\n"); for (int i = 0; i < taille; i++) { th[i] = tirerAuSortHoraire(); } afficherTableauHoraires(th, taille); free(th); th = NULL; } printf("\n"); } void tester3(int taille) { const int seed = 6; srand(seed); struct horaire* th = creerTableauHorairesTiresAuSort(taille); printf("%d %zu %zu\n", taille, sizeof(struct horaire*), sizeof(th)); if (th != NULL) { afficherTableauHoraires(th, taille); free(th); th = NULL; } printf("\n"); } void tester4(int taille) { const int seed = 6; srand(seed); struct horaire** th = creerTableauHoraires(taille); printf("%d %zu %zu\n", taille, sizeof(struct horaire**), sizeof(th)); if (th != NULL) { afficherTableauPointeursHoraire(th, taille); libererTableauHoraires(th, taille); th = NULL; } printf("\n"); } void main(void) { tester1(); tester2(3); tester3(4); tester4(5); }