1. Le fichier d'en-tête : gestion_cours.
h
Ce fichier définit la structure et déclare les fonctions ainsi que le tableau global.
#ifndef GESTION_COURS_H
#define GESTION_COURS_H
#define MAX_COURS 50
#define MAX_NOM_LONG 50
typedef struct {
char nom[50];
char code[15];
int nb_inscrits;
char **etudiants; // Tableau 2D dynamique
} Cours;
// Déclaration externe pour accès dans main.c
extern Cours* catalogue[MAX_COURS];
extern int nb_cours_actuel;
// Prototypes
void ajouterCours();
void supprimerCours();
void rechercherCours();
void afficherTousLesCours();
void libererMemoire();
#endif
2. Le fichier de logique : gestion_cours.c
C'est ici que l'on gère l'allocation dynamique et la manipulation des données.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gestion_cours.h"
// Définition des variables globales
Cours* catalogue[MAX_COURS];
int nb_cours_actuel = 0;
void ajouterCours() {
if (nb_cours_actuel >= MAX_COURS) {
printf("Capacité maximale atteinte.\n");
return;
Cours* nouveau = (Cours*)malloc(sizeof(Cours));
printf("Nom du cours : ");
scanf(" %[^\n]", nouveau->nom);
printf("Code du cours : ");
scanf("%s", nouveau->code);
printf("Nombre d'etudiants : ");
scanf("%d", &nouveau->nb_inscrits);
// Allocation du tableau 2D (lignes = étudiants, colonnes = lettres)
nouveau->etudiants = (char**)malloc(nouveau->nb_inscrits *
sizeof(char*));
for (int i = 0; i < nouveau->nb_inscrits; i++) {
nouveau->etudiants[i] = (char*)malloc(MAX_NOM_LONG *
sizeof(char));
printf("Nom de l'etudiant %d : ", i + 1);
scanf(" %[^\n]", nouveau->etudiants[i]);
catalogue[nb_cours_actuel++] = nouveau;
printf("Cours ajoute avec succes !\n");
void supprimerCours() {
char code[15];
printf("Code du cours a supprimer : ");
scanf("%s", code);
for (int i = 0; i < nb_cours_actuel; i++) {
if (strcmp(catalogue[i]->code, code) == 0) {
// Libération de la mémoire interne
for (int j = 0; j < catalogue[i]->nb_inscrits; j++) {
free(catalogue[i]->etudiants[j]);
free(catalogue[i]->etudiants);
free(catalogue[i]);
// Décalage dans le tableau
for (int k = i; k < nb_cours_actuel - 1; k++) {
catalogue[k] = catalogue[k + 1];
nb_cours_actuel--;
printf("Cours supprime.\n");
return;
printf("Cours non trouve.\n");
void rechercherCours() {
char code[15];
printf("Code du cours recherche : ");
scanf("%s", code);
for (int i = 0; i < nb_cours_actuel; i++) {
if (strcmp(catalogue[i]->code, code) == 0) {
printf("\n--- Infos Cours ---\nCode: %s | Nom: %s\nInscrits: %d\n",
catalogue[i]->code, catalogue[i]->nom, catalogue[i]-
>nb_inscrits);
return;
printf("Aucun cours correspondant.\n");
void afficherTousLesCours() {
if (nb_cours_actuel == 0) {
printf("Aucun cours enregistre.\n");
return;
for (int i = 0; i < nb_cours_actuel; i++) {
printf("\n[%s] %s (%d etudiants)\n", catalogue[i]->code, catalogue[i]-
>nom, catalogue[i]->nb_inscrits);
for (int j = 0; j < catalogue[i]->nb_inscrits; j++) {
printf(" - %s\n", catalogue[i]->etudiants[j]);
3. Le point d'entrée : main.c
Ce fichier contient le menu interactif.
#include <stdio.h>
#include <stdlib.h>
#include "gestion_cours.h"
int main() {
int choix;
do {
printf("\n--- MENU GESTION COURS ---\n");
printf("1. Ajouter un cours\n");
printf("2. Supprimer un cours\n");
printf("3. Rechercher un cours\n");
printf("4. Afficher tous les cours\n");
printf("5. Quitter\n");
printf("Votre choix : ");
scanf("%d", &choix);
switch (choix) {
case 1: ajouterCours(); break;
case 2: supprimerCours(); break;
case 3: rechercherCours(); break;
case 4: afficherTousLesCours(); break;
case 5: printf("Fermeture...\n"); break;
default: printf("Choix invalide.\n");
} while (choix != 5);
return 0;