下一个结构项,类型不完整 [英] Next struct item,incomplete type

查看:47
本文介绍了下一个结构项,类型不完整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct node{  
    struct node next;  
    int id;  
}

给出字段下一个具有不完整的类型错误".

gives "field next has incomplete type error ".

此结构有什么问题?

推荐答案

在创建自引用数据类型时,您需要使用指针来解决圆度问题:

When creating a self-referential data type, you need to use pointers to get around problems of circularity:

struct node;

struct node {  
    struct node * next;  
    int id;  
}

...应该可以,但是使用时请注意正确分配内存.

...should work, but take care to allocate memory correctly when using it.

为什么要使用指针?考虑一下:struct定义的要点是,这样,当您说node.id时,编译器可以算出要分配的内存量和要访问的部分.如果您的node结构包含另一个node结构,编译器应为给定的node分配多少内存?

Why a pointer? Consider this: the point of a struct definition is so that the compiler can figure out how much memory to allocate and what parts to access when you say node.id. If your node struct contains another node struct, how much memory should the compiler allocate for a given node?

通过使用指针,您可以解决此问题,因为编译器知道为指针分配多少空间.

By using a pointer you get around this, because the compiler knows how much space to allocate for a pointer.

这篇关于下一个结构项,类型不完整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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