Segmentation fault: zsh: segmentation fault

Viewed 49

I get a segfault when I add (int filter) in the struct. Is it possible to fix? Error message: zsh: segmentation fault

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

typedef struct Student {
    char name[30];
    char surname[30];
    int course;                 // year of study
    double average;             // average grade
    
    int load;                   // number of courses
    char courses[10][30];       // course names
    int grades[10];             // course grades
    
    
    char languages[100];        // spoken languages
    
    int negative;
//    int filter;
} Student;



int main(int argc, char *argv[]) {
    FILE *db = NULL;
    // open database file for reading, provide a parameter or use default "db.bin"
    if (argc > 1)
        db = fopen(argv[1], "rb");
    else
        db = fopen("db.bin", "rb");
        
    if (db){                            
        Student students[1000];         // all the data goes here
        int size = 0;                   // how many students in database
        
        // reading data from file
        fread(&size, sizeof(int), 1, db);
        
        for (int i = 0; i < size; i++){         
            fread(&students[i], sizeof(Student), 1, db);            
        }   
        printf("%d records loaded succesfully\n", size);
        
        
        // MODIFY CODE BELOW
        
        int counterDemo = 0; // for counting students

        for (int i = 0; i < size; ++i){ // process all the student records in database
            Student s = students[i]; // store data for each student in s
            s.negative = 0;
            // Checking if student has Philosophy and passed Philosophy exam
            for(int j = 0; j < s.load; ++j)
            {
                if(!strcmp(s.courses[j], "Philosophy"))
                {
                    s.negative++;
                }
                    
            }
            bool philosophy_exam = false;
            if(s.negative != 0)    // If student has Philosophy and passed Philosophy exam
            {
                philosophy_exam = true;
            }

            if(philosophy_exam){ // *** first filter, conditions on the student
                int anotherDemo = 0; // for counting courses/grades
                printf("%s %s %d %.2f %d ", s.name, s.surname, s.course, s.average, s.load); // print student data

                for (int i = 0; i < s.load; ++i){ // process each course taken by the student
                    if(1){ // *** second filter, conditions on the course/grade
                        ++anotherDemo; // counting courses
                        printf("%s %d ", s.courses[i], s.grades[i]);
                    }
                }
                printf("%s\n", s.languages);
                
                if (anotherDemo == s.load) // *** third filter, various other conditions            
                    ++counterDemo; // counting studfents
            }
        }
        printf("Filter applied, %d students found\n", counterDemo); // how many passed the filters
        fclose(db); 
    } else {
        printf("File db.bin not found, check current folder\n");
    }

    return 0;
}

I commented the problematic variable out. Also I run my code on terminal, like always. I am using (clang "file" -o main) could this be the problem? I have tried to run this code in different apps, but it doesn't help.

0 Answers
Related