简体   繁体   English

向数组中添加 1 个以上项目时,C 程序中出现分段错误

[英]Segmentation error in C program when adding more than 1 item to an array

I'm trying to build a program that has a student id array, a course code array of char pointers to string literals, and a registration table 2d array to hold a 1 or 0 whether the student is enrolled in a course or not.我正在尝试构建一个程序,它有一个学生 id 数组、一个指向字符串文字的 char 指针的课程代码数组,以及一个注册表 2d 数组,无论学生是否注册课程,都可以保存 1 或 0。 So far I am at the point where I am adding the students which works fine.到目前为止,我正在添加工作正常的学生。 The program also runs as expected when I input a total of 1 course.当我总共输入 1 个课程时,该程序也按预期运行。 The issue however is when I try to add more than 1 course code to the courseArray, I'm getting a segmentation error after inputting the codes.然而,问题是当我尝试向 courseArray 添加 1 个以上的课程代码时,输​​入代码后出现分段错误。 I'm unsure how to fix this and no resource on the matter seems to point me in the right direction.我不确定如何解决这个问题,并且没有关于此事的资源似乎为我指明了正确的方向。 Any help with this is appreciated.对此的任何帮助表示赞赏。

#include <stdio.h>
#include "Functions.h"
#include <stdbool.h>

int main()
{
    //declaring variables for number of students and courses

    int numStudents, numCourses;

    //declaring 2d array for the registration table

    int registrationTable[numStudents][numCourses];

    //prompting user input for number of students and storing in numStudents

    printf("How many students would you like to register: \n");
    scanf("%d", &numStudents);

    //create student array based on numStudents

    int studentArray[numStudents];

    //Prompting user input for id's of students in student array

    for (int i = 0; i < numStudents; i++)
    {
        printf("Please enter the student ID for student %d: \n", i + 1);
        scanf("%d", &studentArray[i]);
    }

    //Prompting user input for number of courses offered and storing in numCourses

    printf("How many courses are you offering: \n");
    scanf("%d", &numCourses);

    //create couses array based on numCouses

    char *courseArray[numCourses];

    //Prompting user input for course codes in course array

    for (int i = 0; i < numCourses; i++)
    {
        printf("Please enter the course code for course %d: \n", i + 1);
        scanf(" %s", courseArray[i]);
    }

    validate(studentArray, courseArray, numStudents, numCourses);

return 0;
}

When you declare the registrationTable array, numStudents and numCourses are uninitialized.当您声明registrationTable数组时, numStudentsnumCourses未初始化。 This invokes undefined behavior .这会调用未定义的行为

Using scanf later to read values into these variables does not alter the declaration of the array.稍后使用scanf将值读入这些变量不会改变数组的声明。

However, it doesn't appear you use this array, so this is not causing your segmentation fault.但是,您似乎没有使用此数组,因此这不会导致您的分段错误。

Your problem stems from:你的问题源于:

char *courseArray[numCourses];

And then reading into that array of strings.然后读入该字符串数组。

    for (int i = 0; i < numCourses; i++)
    {
        printf("Please enter the course code for course %d: \n", i + 1);
        scanf(" %s", courseArray[i]);
    }

You have declared an array of char pointers, but these pointers do not themselves point to anything.您已经声明了一个char指针数组,但这些指针本身并不指向任何东西。 You would either need to use a 2-dimensional array instead like char courseArray[numCourses][100] or use malloc to dynamically allocate the memory for each pointer to point to.您要么需要使用二维数组,如char courseArray[numCourses][100]或使用malloc为每个指向的指针动态分配内存。

Should you choose to use malloc , you will need to remember to free each string in the array as well.如果您选择使用malloc ,您还需要记住free数组中的每个字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM