segmentation fault while assigning structure members in c

☆樱花仙子☆ 提交于 2019-12-31 07:29:07

问题


I have two structure in 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; 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; d has the name already :) thanks all


回答1:


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).

Your segmentation violation is almost certainly caused by an uninitialized pointer.




回答2:


I can be caused by a member pointing into an invalid zone.




回答3:


l->data is most likely NULL



来源:https://stackoverflow.com/questions/1935642/segmentation-fault-while-assigning-structure-members-in-c

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