I'm trying to make an array of structs by using malloc to allocate the memory needed like so:
typedef struct stud{
char stud_id[MAX_STR_LEN];
char stud_name[MAX_STR_LEN];
Grade* grd_list;
Income* inc_list;
}Stud;
Stud* students = malloc(sizeof(Stud)*STUDENT_SIZE);
The problem is that I have a function that adds id and name to a place in the array like so:
void new_student(Stud* students[], int stud_loc){
scanf("%s", students[stud_loc]->stud_id);
printf("%s", students[stud_loc]->stud_id);
scanf("%s", students[stud_loc]->stud_name);
printf("%s", students[stud_loc]->stud_name);
}
But after the first call to the function, which works, the second one gives me the error:
Segmentation fault (core dumped)
And I can only think that it must mean I'm not doing this right and all the memory is probably going into one place and not in an array form. I'd rather do
Stud students[STUDENT_SIZE];
but in this case I must use malloc.
I tried using calloc but I still get the same error.
malloc()?Stud*value to a function that takesStud*[].new_student(Stud* students[], ...is equal tonew_student(Stud** students, .... But you wantnew_student(Stud* students,.... The compiler should have warned you about this.