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

C语言链表有关问题无情的报错了

2013-10-11 
C语言链表问题无情的报错了本帖最后由 Benjaminzhou93 于 2013-10-10 22:30:24 编辑typedef struct{int n

C语言链表问题无情的报错了
本帖最后由 Benjaminzhou93 于 2013-10-10 22:30:24 编辑 typedef struct
{
int n;
struct list* next;
}list;

void main()
{
list lst, a, b, c, *d;
lst.next = &a;
a.next = &b;
b.next = &c;
d = lst.next->next;//13行
}


E:\Windows工作区\test3.c(10) : warning C4133: '=' : incompatible types - from 'struct list *' to 'struct list *'
E:\Windows工作区\test3.c(11) : warning C4133: '=' : incompatible types - from 'struct list *' to 'struct list *'
E:\Windows工作区\test3.c(12) : warning C4133: '=' : incompatible types - from 'struct list *' to 'struct list *'
E:\Windows工作区\test3.c(13) : error C2037: left of 'next' specifies undefined struct/union 'list'
执行 cl.exe 时出错.

[解决办法]
本帖最后由 max_min_ 于 2013-10-10 22:32:28 编辑


//定义改成吧
typedef struct _list_tags
{
int n;
struct _list_tags* next;
}list;

看看这个再写,用理论去实践
http://blog.csdn.net/feixiaoxing/article/details/6849457
[解决办法]
结构体定义的时候少写了一个list,导致struct list不能识别,应该这样:

typedef struct list
{
int n;
struct list* next;
}list;


引用:
typedef struct
{
int n;
struct list* next;
}list;

void main()
{
list lst, a, b, c, *d;
lst.next = &a;
a.next = &b;
b.next = &c;
d = lst.next->next;//13行
}


E:\Windows工作区\test3.c(10) : warning C4133: '=' : incompatible types - from 'struct list *' to 'struct list *'
E:\Windows工作区\test3.c(11) : warning C4133: '=' : incompatible types - from 'struct list *' to 'struct list *'
E:\Windows工作区\test3.c(12) : warning C4133: '=' : incompatible types - from 'struct list *' to 'struct list *'
E:\Windows工作区\test3.c(13) : error C2037: left of 'next' specifies undefined struct/union 'list'
执行 cl.exe 时出错.

[解决办法]
楼主的代码编译不通过,d = lst.next->next;这一句有问题,我把程序修改了一下(基本上只是加了一个 list),如下:
#include <stdio.h>

typedef struct list
{
    int n;
    struct list *next;
} list;

int main()
{
    list lst, a, b, c, *d;
    lst.next = &a;
    a.next = &b;
    b.next = &c;
    d = lst.next->next;
    return 0;
}

以上代码codeblocks编译通过
请在定义结构体时加上list,否则IDE会误解 struct list *next;这一句

热点排行