C语言学习笔记——段错误

typedef struct shuju
{
	int id;
	char *text;
	void *data;
	struct shuju *next;
}shuju,*shujuptr;

shujuptr newer;
newer->id = 1;

这是调试会发生段错误,因为没有给结构体分配内存,需要在前面为其分配内存:

typedef struct shuju
{
	int id;
	char *text;
	void *data;
	struct shuju *next;
}shuju,*shujuptr;

shujuptr newer;
newer = (shujuptr)malloc(sizeof(shuju));
newer->id = 1;
typedef struct shuju
{
	int id;
	char *text;
	void *data;
	struct shuju *next;
}shuju,*shujuptr;

shujuptr newer;
newer = (shujuptr)malloc(sizeof(shuju));
newer->id = 1;

typedef struct Data
{
	char *in;
	char *out;
}Data;
(Data*)((new->data))->in = "input";

这里也会发生段错误,即使给new这个结构体分配了内存,但是它的成员data也需要分配内存

typedef struct shuju
{
	int id;
	char *text;
	void *data;
	struct shuju *next;
}shuju,*shujuptr;

shujuptr newer;
newer = (shujuptr)malloc(sizeof(shuju));
newer->id = 1;

typedef struct Data
{
	char *in;
	char *out;
}Data;
newer->data = (Data*)malloc(sizeof(Data));
(Data*)((newer->data))->in = "input";

你可能感兴趣的:(C/C++学习笔记,深度学习,pytorch,机器学习)