首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

C语言链表的定义,该怎么解决

2013-10-12 
C语言链表的定义typedef struct aaa{int istruct AAA* next}AAAvoid test(){AAA *p (AAA*)calloc(1,

C语言链表的定义
typedef struct aaa
{
int i;
struct AAA* next;
}AAA;

void test()
{
AAA *p = (AAA*)calloc(1, sizeof(AAA));
p->next = (AAA*)calloc(1, sizeof(AAA));
}
为什么p->next = (AAA*)calloc(1, sizeof(AAA)) 
提示 Error:不能将(AAA *)类型的值分配到(AAA *)类型的实体

如果定义为
typedef struct aaa
{
int i;
struct AAA* next;
}AAA;
就不会出现上述提示
[解决办法]
其实是你写错了!
struct AAA 是不存在的。
你有两种方案供选择
1.不使用别名,简单明了。我的建议是以后多敲个struct,少用别名typedef,这样能保证程序明了性,否则,会隐藏数据类型真实面目,只会增加阅读代码的复杂性。如果你看linux内核源码的话,你会体会到我的建议。


struct aaa{
struct aaa *next;
int i;
};

struct aaa *p = malloc(sizeof(struct aaa));
p->next = mallco(sizeof(struct aaa));


2.可能你会不接受上面这种方案,那么就依然使用别名
typedef struct aaa AAA;
typedef stru
...
即5l的方式

热点排行