I am trying to use a scan function that can basically iterate through nodes inside of my program. I want it to read First then Last and move onto the next student. It compiled correctly but gives a segmentation fault after I input the first name.
#include <stdio.h>
struct student{
char firstname[70];
char lastname[70];
struct student *next;
};
void printstudentinfo(struct student *thestudent){
printf("The info of your students is the following: \n");
int i =1;
while(thestudent!=NULL){
printf("Info of student %d is: \n",i);
printf("First name: %s\n", thestudent->firstname);
printf("Last name: %s\n", thestudent->lastname);
thestudent = thestudent->next;
return;
}
}
void scaninfo(struct student *thestudent){
struct student *stud;
printf("Enter the information of your students down below: \n");
while(thestudent!=NULL){
printf("Please enter the first name: \n");
scanf("%s",stud->firstname);
printf("Please enter the last name: \n");
scanf("%s",stud->lastname);
}
return;
}
int main(){
struct student s1, s2, s3, s4;
s1.next = &s2;
s2.next = &s3;
s3.next = &s4;
s4.next = NULL;
scaninfo(&s1);
printstudentinfo(&s1);
return 0;
}