Program received signal SIGSEGV, Segmentation fault. when debugging

对着背影说爱祢 提交于 2019-12-04 06:29:06

问题


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.


回答1:


You need to allocate memory for term_ptr->name




回答2:


Change this:

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

to this:

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


来源:https://stackoverflow.com/questions/24851912/program-received-signal-sigsegv-segmentation-fault-when-debugging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!