简体   繁体   中英

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. 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. 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. 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. This invokes undefined behavior .

Using scanf later to read values into these variables does not alter the declaration of the array.

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. 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.

Should you choose to use malloc , you will need to remember to free each string in the array as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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