0

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;
}
Ôrel
  • 7,044
  • 3
  • 27
  • 46
  • You have to allocate memory for `stud` – Ôrel Jul 19 '20 at 21:38
  • First of all, indent your code properly. Next, read [this](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings). This will not resolve all the problems bit it's a start. – n. m. could be an AI Jul 19 '20 at 21:39

2 Answers2

1

For printstudentinfo just move the return

For scaninfo just do the same as printstudentinfo, use the function argument and remove the stud variable

#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){
    printf("Enter the information of your students down below: \n");
    while(thestudent!=NULL){
        printf("Please enter the first name: \n");
        scanf("%s",thestudent->firstname);
        printf("Please enter the last name: \n");
        scanf("%s",thestudent->lastname);
        thestudent = thestudent->next;
    }
    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;
}
Ôrel
  • 7,044
  • 3
  • 27
  • 46
0

It goes wrong in:

struct student *stud;
scanf("%s",stud->firstname);
scanf("%s",stud->lastname);

as a solution you can change this to

struct student stud;
scanf("%s",stud.firstname);
scanf("%s",stud.lastname);

it should work for the segmentation (It probably doesn't still do what you want)