动态列表列表的分段错误

时间:2014-01-14 17:10:27

标签: c arrays list dynamic

typedef struct LabSection {
    StudentInfoT *head;
    int starting_hour;
    working_days day;
} LabSectionT;



typedef struct StudentInfo {
    char* name;
    int tmhma;
    struct StudentInfo* next;
} StudentInfoT;

我有这两个结构,我想用这个函数创建一个动态的实验室数组

LabSectionT* DataInsert (int *sections_number) {
    int students_number, max_lab_size;
    int how_many_sections;
    LabSectionT *labs_array;
    int i;
    char student_name[MAX_STUDENT_NAME_SIZE];
    char formatstring[13];
    sprintf(formatstring, "%%%ds",MAX_STUDENT_NAME_SIZE -1);
    int section,time,day;

    printf("Enter number of students: ");
    scanf("%d", &students_number);
    printf("Enter maximum lab size: ");
    scanf("%d", &max_lab_size);

    LabSectionT* labs_head = NULL;


    how_many_sections = students_number/max_lab_size; 
    labs_array = (LabSectionT*)malloc(how_many_sections * sizeof(LabSectionT));
    if (labs_array == NULL) {
        printf("Couldn't allocate memory for labs\n");
        exit(1);
    }
    labs_head = labs_array;

    for ( i = 0, time = 8, day = Mon; i < how_many_sections; i++,time++) {
        if (time > 21) {
            time = 8;
            day++;
        }

        if ((day >= Mon ) && (day <= Fri) && (time >= 8) && (time <= 21)) {
            labs_array[i].starting_hour = time;
            labs_array[i].day = day;
        }
    }
    if (debug == ON) {
        printf("\n>>Created %d sections<<\n", how_many_sections);
    }
    sections_number = &how_many_sections;

    for (i = 0; i < how_many_sections; i++) {
        labs_array[i].head = NULL; 
    }

    StudentInfoT* new_student;

    for (i = 0; i < students_number; i++) {
        new_student = NULL;
        printf("Enter student name: ");
        scanf(formatstring, student_name);
        new_student = (StudentInfoT*)malloc(sizeof(StudentInfoT));
        if (new_student == NULL){
            printf("Couldn't allocate memory for new student\n");
        }
        new_student->name=student_name;
        section = i % max_lab_size;
        InsertStudent(labs_array, new_student, section);



    }
    return(labs_head);

}

当学生人数超过max_lab_size时,我会创建一个新的实验室部分 当我尝试使用PrintSections(sections_head, *sections_number)打印该部分时,我遇到了分段错误; 编辑:当我尝试在打印功能

中使用数组的地址时,会发生分段

我认为问题是数组将是** LabSectionT

我是对的吗?

0 个答案:

没有答案