Segmentation Fault on -> and strcpy line
Hi people,
Im a newbie in here so please bare with my lack of C knowledge. I'm currently trying to read from a file and storing its information to an array of a structure. Obviously I'm getting an error (Segmentation fault). I know its the -> sign and the strcpy line but Im just clue less on how to fix it. The code is below.
Code:
void input_students(student_type * students[], int * n, FILE* ifp1){
//declare variables here
char last_name[26];
//last_name = (char *) malloc( sizeof(char) );
int std_no; // integer student number
char major[16]; //the major
int num_of_courses; //the num of courses the person is taking
char course_nums[8][7];
int course_marks[8];
*n=0;
int i=0;
int j;
while (fscanf(ifp1, "%s", last_name) != EOF){
students[j]= (student_type*)malloc(sizeof(student_type));
strcpy(*(students[j]->last_name), last_name);
fscanf(ifp1 , "%d %s %d", &students[j]->std_no, &students[j]->major,
&students[j]->num_of_courses);
for(i=0; i < students[j]->num_of_courses; i++){
fscanf(ifp1, "%s", &students[j]->course_nums[i]);
fscanf(ifp1, "%d", &students[j]->course_marks[i]);
} j++;
}
*n = j;
}
Any help would be great. Thank you.