分段故障而在C分配结构成员 [英] segmentation fault while assigning structure members in c

查看:105
本文介绍了分段故障而在C分配结构成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C两个结构

struct data{
  char *name;
};

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

当我试图像分配

1->数据 - >名称= D->名称;
的printf(%S,1->数据 - >名);

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 :

1->数据= D;
D的名字已经:)感谢所有

l->data = d; d has the name already :) thanks all

推荐答案

在做细分违章,造成指令,只插入前:

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

,看看哪一个设置为NULL(或一个无效的值)。

and see which one is set to NULL (or an invalid value).

您段违规几乎可以肯定是由未初始化的指针导致的。

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

这篇关于分段故障而在C分配结构成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆