0

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;
}
Turk
  • 17
  • 4
  • You use `numStudents` and `numCourses` before given them a value (`int registrationTable[numStudents][numCourses];`) – ikegami Oct 17 '21 at 21:34
  • But the problem you're asking about is that you're using `courseArray[i]` before assigning a value to it (`scanf(" %s", courseArray[i]);`). `scanf %s` expects a pointer to memory in which it can store a string, but you attempt to read uninitialized memory instead. `char courseArray[numCourses][MAX_COURSE_NAME_LEN + 1];` – ikegami Oct 17 '21 at 21:36
  • `char *courseArray[numCourses]` allocates an array of pointers to char, not an array of c-strings. You need to initialize that array of pointers to point at allocated storage. Maybe consider using a 2 dimensional array of `char` instead – infixed Oct 17 '21 at 21:38
  • @ikegami I havent used registrationTable yet in my program so thanks for pointing that out. I think I might understand the issue, another user mentioned using malloc to create the space in memory, is this whats necessary in this case? If so Im just confused why everything works when I only create 1 pointer in the course array. – Turk Oct 17 '21 at 21:49
  • Re "*i I havent used registrationTable yet in my program so thanks for pointing that out.*", Even so, using it as you did is Undefined Behaviour and therefore sufficient to crash the program. – ikegami Oct 17 '21 at 21:49
  • Re "*another user mentioned using malloc to create the space in memory*", Not much point to allocating each string separately if they're all going to be of the same size. So I'd use what I showed instead of `malloc` if using `scanf`. (I'd use `getline` instead of `scanf`, but that's just my preference.) – ikegami Oct 17 '21 at 21:52
  • @ikegami I understand, it's a program for school so I am limited to what I can use. I've seen many times that scanf is not preferred and I'm not quite sure why I'm being pushed to use it. Also I need the course array to be 1d so that's why I'm using the char pointer method. – Turk Oct 17 '21 at 21:58
  • Then yes, you need `malloc`. Though you should tell `scanf` the maximum it can write to the buffer. – ikegami Oct 17 '21 at 22:00

1 Answers1

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.

Chris
  • 26,361
  • 5
  • 21
  • 42
  • I see, so malloc is necessary in this case? Why does the block of code allow a single course code to be entered without issue using the same format that I have? thanks. – Turk Oct 17 '21 at 21:43
  • Re "*I see, so malloc is necessary in this case?*", The answer gives an alternative to using `malloc`, and so does an earlier comment... – ikegami Oct 17 '21 at 21:55