я не знаю, почему печать моего массива дает ошибку сегментации? - PullRequest
0 голосов
/ 17 января 2020

Я хочу напечатать массив структур, которые заполнит пользователь, но я получаю сообщение об ошибке, когда программа достигает блока печати. Вот код: я определил три структуры следующим образом:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define nbmodmax 7
#define nbcarmax 10
#define nbetdmax 300
struct DN{
    int jj;
    int mm;
    int aaaa;
};
struct modules{
    int nbmod;
    char mod[nbmodmax][nbcarmax];
};
struct Etudiant{
    char *nom;
    char *prenom;
    struct DN date;
    int cne;
    struct modules mods;
};

в основной функции я попросил пользователя указать размер массива структуры smi4 [] и заполнить каждое поле:

main()
{
    int nb,i,j,posmax;
    struct Etudiant tmp,smi4[nbetdmax];
    printf("entrer le nb des etudiants\n");
    scanf("%d",&nb);
    for(i=0;i<nb;i++)
    {
        printf("\nCoord. du %dème etudiant\n",i+1);
        printf("\n nom: ");
        smi4[i].nom=(char*)malloc(20);
        scanf("%s",&smi4[i].nom);
        printf("\n prenom: ");
        smi4[i].prenom=(char*)malloc(20);
        scanf("%s",&smi4[i].prenom);
        printf("\n date de naissance \n");
        printf("\n jour : ");
        scanf("%d",&smi4[i].date.jj);
        printf("\n mois : ");
        scanf("%d",&smi4[i].date.mm);
        printf("\n année : ");
        scanf("%d",&smi4[i].date.aaaa);
        printf("\n CNE : ");
        scanf("%d",&smi4[i].cne);
        printf("\n donner la liste des modules\n");
        printf("\n Nombre de modules : ");
        scanf("%d",&smi4[i].mods.nbmod);
            for(j=0;j<smi4[i].mods.nbmod;j++)
                {
                    printf("\n module %d : ",j+1);
                    scanf("%s",&smi4[i].mods.mod[j]);
                }
    }

до сих пор все работает хорошо, но следующий блок генерирует ошибку:

 i=j=0;
    printf("\n ---- list ---- \n");
    printf("\n nom: %s ",smi4[0].nom);
    for(i=0;i<nb;i++)
    {
        printf("\nCoord. du %dème étudiant\n",i+1);
        printf("\n nom: %s ",smi4[i].nom);
        printf("\n prenom: %s ",smi4[i].prenom);
        printf("\n date de naissance \n");
        printf("\n jour : %d",smi4[i].date.jj);
        printf("\n mois : %d",smi4[i].date.mm);
        printf("\n année : %d ",smi4[i].date.aaaa);
        printf("\n CNE : %d ",smi4[i].cne);
        printf("\nla liste des modules\n");
        printf("\n Nombre de modules : %d ",smi4[i].mods.nbmod);
            for(j=0;j<smi4[i].mods.nbmod;j++)
                {
                    printf("\n module %d : %s ",j+1,smi4[i].mods.mod[j]);
                }
    }
  }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...