在c中分配结构成员时出现分段错误 [英] segmentation fault while assigning structure members in c

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

问题描述

我在 c 中有两个结构

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

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->数据 = d;d 已经有名字了 :) 谢谢大家

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

推荐答案

就在你执行导致分段违规的指令之前,插入:

Just before you do that segmentation-violation-causing instruction, insert:

printf( "%p
", l);
printf( "%p
", l->data);
printf( "%p
", d);
printf( "%p
", 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天全站免登陆