0
votes

If I debug my code then I get "Program received signal SIGSEGV, Segmentation fault." Here is my code-

#include <stdio.h>
#include <conio.h>
#include <string.h>

int main()
{
    struct term
    {
        char* name;
        long int id;
        float term_gpa;
    };
    struct term *term_ptr, student;

    term_ptr = &student;

    strcpy( term_ptr->name,"niton");
    term_ptr->id = 942044;
    term_ptr->term_gpa = 3.75;

    printf("Name : %s",term_ptr->name);
    printf("Name : %s",student.name);

    getch();
    return 0;
}

I get this error on line 17. Please help me! Sorry for my bad English.

2

2 Answers

2
votes

You need to allocate memory for term_ptr->name

1
votes

Change this:

strcpy( term_ptr->name,"niton");

to this:

term_ptr->name = strdup("niton");