Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 动态列表数组的分段错误_C_Arrays_List_Dynamic - Fatal编程技术网

C 动态列表数组的分段错误

C 动态列表数组的分段错误,c,arrays,list,dynamic,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;

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

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



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

当学生人数超过最大实验室规模时,我会创建一个新的实验室部分 当我试图使用PrintSectionssections\u head、*sections\u number打印节时,我遇到了分段错误; 编辑:当我尝试在print函数中使用数组的地址时,会发生分段

我认为问题在于数组将是**labsection t


我说的对吗?

您是否已在调试器中仔细检查了代码并检查了数据?你很可能在某个地方有一个空指针。你知道,除非学生数是最大实验室大小的完美倍数,否则你会得到整数除法舍入,对吗?因此,在一个30人大小的实验室里,40名学生将。。第一,让10个学生在寒冷中呆着。不知道头是什么,没有办法确定,但我猜这就是你分配动态数组的变量。在这种情况下:否。将指针传递给函数正好意味着:被调用函数接收变量的值,在指针的情况下,变量的值是内存地址。但是,您的代码正在左右分配内存,但从未释放内存。我也不明白labs_head变量的意义。。。我也看不到将指向int的指针传递给函数的意义……哦,像往常一样,不要强制转换void*malloc返回,不要使用数组和指针,好像它们的意思是一样的:它们不是。回答你的问题:否:像以往一样,传递指针就是传递该变量的值。对于指针,该值恰好是内存中的一个地址,要将指针传递给期望指针的函数,必须传递&some_ptr_var指针变量的地址当学生人数超过max_lab_size时,我创建一个新的实验部分。我在函数中传递指向int的指针,因为我需要函数在不返回该值的情况下更改该值。谢谢你的指点,我忘了。
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);

}