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

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

问题描述

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天全站免登陆