简体   繁体   English

在c中分配结构成员时出现分段错误

[英]segmentation fault while assigning structure members in c

I have two structure in c 我在C中有两个结构

struct data{
  char *name;
};

struct lst{
   struct lst *next;
   struct table *data;
};

when I'm trying to assign a name like 当我尝试分配一个名称时

l->data->name = d->name; l->数据->名称= d->名称; printf("%s",l->data->name); printf(“%s”,l-> data-> name);

it gives segmentation fault. 它给出了分割错误。 So is it because read-only memory or caused by another reason ? 难道是因为只读内存还是其他原因引起的?

ok I solved the problem : ) I've done : 好的,我解决了问题:)我已经完成了:

l->data = d; l->数据= d; d has the name already :) thanks all d已经有了名字:)谢谢大家

Just before you do that segmentation-violation-causing instruction, insert: 在执行导致细分违规的指令之前,请插入:

printf( "%p\n", l);
printf( "%p\n", l->data);
printf( "%p\n", d);
printf( "%p\n", d->name);

and see which one is set to NULL (or an invalid value). 并查看将哪一个设置为NULL(或无效值)。

Your segmentation violation is almost certainly caused by an uninitialized pointer. 您的细分违规行为几乎可以肯定是由未初始化的指针引起的。

我可能是由成员指向无效区域引起的。

l->data最有可能为NULL

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

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