Hey all, I am a C beginner. My problem with my code is that I get a segfault.
Here's my code
#include <stdio.h>
struct studentInfo {
int totalClasses;
float GPA;
char studentName[41];
};
main()
{
int numOfStudents, i;
printf("How many students would you like to take account of? ");
scanf(" %d", &numOfStudents);
struct studentInfo *students[numOfStudents-1];
for (i=0; i < numOfStudents; i++)
{
printf("Enter the total number of the student's classes: ");
scanf(" %d", &students[i]->totalClasses);
printf("Enter the student's GPA: ");
scanf(" %f", &students[i]->GPA);
getchar(); /* Clear the extra keypress in the buffer */
printf("Enter the student's name: ");
fgets(students[i]->studentName, 41, stdin);
printf("\n\n");
}
printf("\n\n");
for (i=0; i < numOfStudents; i++)
{
printf("Student Name: %s", students[i]->studentName);
printf("Student GPA: %.3f\n", students[i]->GPA);
printf("Number of Student Classes: %d\n\n", students[i]->totalClasses);
}
printf("\n\n");
printf("Goodbye.\n");
return 0;
}
According to gdb the segfault happens on:
scanf(" %d", &students[i]->totalClasses);
But I don't know what I am doing wrong. Any help?