Hello fellow C programmers. I am trying to write a program for homework and am having trouble when creating the basic foundation for this assignment. When I try to compile I get these errors:
Code:
j = 0
student[0]->first_name = SAM
student[0]->last_name = MALONE
student[0]->month = MAY
student[0]->day = 3
student[0]->year = 1961
j = 1
Segmentation fault: 11
First, I need to use a struct, so there is no way around that. My logic is that I need to increase the number in the bracket attached to student so that further down the road in my program I can compare the students (eg. Without the number in the bracket, I can't think of a way to compare them). Can anyone guide me on this seg fault? I tried to debug by printing out `j` and it looks as if it makes one full loop without trouble and then it crashes when it tries to increase `student[0]` to `student[1]`
Any help would be greatly appreciated!
Code:
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# define MAX_CHARS_IN_NAME 30
typedef struct
{
char *first_name;
char *last_name;
char month[10];
int day;
int year;
} person;
int main()
{
// Create a file pointer and open the file
FILE *ifp = fopen("birthday.txt", "r");
// Test to see if the file opens
if (!ifp)
{
printf("The file is unable to open!\n");
return -1;
}
// Create variables for the number of classes, number of students in the class, birthday month/day/year, number of queried students, queried students
int i, num_classes, num_students, num_of_queried;
char queried_student;
// Read in the number of classes. PROPERLY READ IN (TESTED)
fscanf(ifp, "%d", &num_classes);
// Read in the number of students in the class. PROPERLY READ IN (TESTED)
fscanf(ifp, "%d", &num_students);
// Allocating space for the students in the classroom
person *student = malloc(sizeof(person));
student->first_name = malloc(sizeof(char) * MAX_CHARS_IN_NAME);
student->last_name = malloc(sizeof(char) * MAX_CHARS_IN_NAME);
/*
// Allocating space for the queried student
person *queried_student = malloc(sizeof(person));
queried_student->first_name = malloc(sizeof(char) *MAX_CHARS_IN_NAME);
queried_student->last_name = malloc(sizeof(char) *MAX_CHARS_IN_NAME);
*/
// Loop through the text file
for (i=0; i< num_classes; i++)
{
int j;
for (j=0; j< num_students; j++)
{
printf("j = %d\n", j);
// Scan in the student's first name
fscanf(ifp, "%s", student[j].first_name);
printf("student[%d]->first_name = %s\n", j, student->first_name);
// Scan in the student's last name
fscanf(ifp, "%s", student[j].last_name);
printf("student[%d]->last_name = %s\n", j, student->last_name);
// Scan in the student's birth month
fscanf(ifp, "%s", student[j].month);
printf("student[%d]->month = %s\n", j, student->month);
// Scan in the student's birth day
fscanf(ifp, "%d", &student[j].day);
printf("student[%d]->day = %d\n", j, student->day);
// Scan in the student's birth year
fscanf(ifp, "%d", &student[j].year);
printf("student[%d]->year = %d\n", j, student->year);
// Scan in the number of queried students
//fscanf(ifp, "%d", &num_of_queried);
/* int k;
for (k=0; k< num_of_queried; k++)
{
} */
}
}
// Sort the list in order of birthday, IGNORING THE YEAR!!!
// print output
fclose(ifp);
return 0;
}