问题
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