简体   繁体   English

程序收到信号SIGSEGV,分段故障。 调试时

[英]Program received signal SIGSEGV, Segmentation fault. when debugging

If I debug my code then I get "Program received signal SIGSEGV, Segmentation fault." 如果调试代码,则会收到“程序收到信号SIGSEGV,分段错误”。 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! 我在第17行收到此错误。请帮助我! Sorry for my bad English. 对不起,我的英语不好。

您需要为term_ptr->name分配内存

Change this: 更改此:

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

to this: 对此:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM