0

I have a linked list which stores student's information. The list includes one char * and two int, my problem arises when I try to initialize my char * with a string, as I get Seg Fault when I do so.

Things I've tried: using strcpy instead of = as well as dynamically allocating memory.

typedef struct Student{
    int age,id;
    char *name;
    struct Student *next;
}Student;

struct Student *head = NULL;

Create linked list function

Student *create (){
    char name[128];

    Student *newStudent = (Student*)malloc(sizeof(Student));
    newStudent->name = malloc(sizeof(Student) + 1);
    newStudent->age = (rand() % (35 - 18 + 1)) + 18; // age range of student from 18-35 
    newStudent->id = rand() % 1000000 + 100000; // 6 digit id 

    scanf("%127s", name);
    strcpy(newStudent->name, name);

    newStudent->next = NULL;
    return newStudent;
}

insert function

Student *insert(Student *newStudent){
    Student *ptr = head;

    if (head == NULL)
        return newStudent;

    while (ptr->next != NULL){
        ptr = ptr->next;
    }
    ptr->next = newStudent;
    return head;
}

Build function

Student *build(){
    int size;
    Student *newStudent = (Student*)malloc(sizeof(Student));

    printf("Enter size of linked list: ");
    scanf("%d",&size);

    for (int i=0; i<size; i++){
        newStudent = create();
        head = insert(newStudent);
    }
    return head;
}

print and main function

void print(Student *head){
    for (Student *ptr = head; ptr != NULL; ptr = ptr->next)
        printf("Student info\nName: %s, ID: %d, age: %d\n", ptr->name, ptr->id, ptr->age);

    printf("\n");
}
int main(){
    srand(time(0));

    head = build();
    print(head);
    return 0;
}
1
  • 3
    It looks like you should not be using union for the name and age/id fields in this case. union will not store all three members separately. Instead it will store members one at a time in one location. So when you set u.age you will erase u.name or vice-versa. Commented Feb 26, 2021 at 23:24

1 Answer 1

3

char *name; strlen(name); is undefined behavior, and a segfault is a reasonable expectation. Similarly for char *name; scanf("%s", name);

In both of those cases, name is uninitialized (so you can think of it as addressing nowhere.) When you try to compute the length of the string at nowhere, you get an error. Similarly when you try to use scanf to write some data to nowhere. Perhaps you want:

char name[128]; scanf("%127s", name);

or similar. Whatever you do, you need name to reference a valid memory location before you use it. Whether you declare it as an array or declare it as a pointer and assign it an address with malloc doesn't really matter. (Making it an array is often easier, since you don't need to worry about freeing it.)

Sign up to request clarification or add additional context in comments.

3 Comments

I fixed my code, to include you're input and a comment about removing union, but I still get seg faults.
Now you have the same problem with strcpy(newStudent->name, name); You have not initialized newStudent->name, so trying to copy data to that address is invalid. Try another malloc, or struct Student{ int age,id; char name[128]; struct Student *next; }
Thanks! I got it to work after allocating newStudent->name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.