GCC:数组类型具有不完整的元素类型? [英] GCC: array type has incomplete element type?

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

问题描述

GCC给了我一个数组类型具有不完整的元素类型误差消息当我尝试编译如下:

  typedef结构_node节点;
结构_node {
 INT富;
 节点(*儿童)[2];
 INT吧;
};

在内存中的结构应该是这样的。

  0x345345000000富
0x345345000004指针1子节点
0x345345000008指针2.子节点
0x34534500000C酒吧


解决方案

这是因为C型前pression的工作倒过来。

 节点(*儿童)[2];

全文如下:儿童的内容,提领时( * 适用于它们),产量一件对数组运算符 [] 可以应用,产生了节点。这意味着孩子是一个指向的两个数组节点。但是,你不想要一个指向数组的指针,你希望两个指针数组。对于这一点,你应该把括号这样的:

 节点*(儿童[2]);

然后内容:孩子的内容是两个值的数组;应用 * 就在其中产生了节点。这就是你想要什么:两个指针数组节点。注意,这相当于:

 节点*儿童[2];

因为在C语法,当它涉及到输入前pressions,在 [] 经营者接管precedence的 * 运营商。

在您最初的版本中,C编译器抱怨,因为当时它读取的声明,它不知道什么尺寸节点终止(结构定义尚未完成),正因为如此,有困难想象一个类型两节点阵列。这是问题的一个比较间接的症状。

旁注:你应该使用 _node 或任何其它标识符开始在你的code下划线避免。这些标识符是保留给实施。在实践中,系统头可以使用它们,和碰撞可能被证明是有害的。就个人而言,我倾向于在的结束的,像这样的加下划线: typedef结构node_节点;

GCC gives me an "array type has incomplete element type"-error message when i try to compile this:

typedef struct _node node;
struct _node{
 int foo;
 node (*children)[2];
 int bar;
};

In memory the struct should look like this

0x345345000000 foo
0x345345000004 pointer to 1. child node
0x345345000008 pointer to 2. child node
0x34534500000C bar

解决方案

That's because the C type expression works the other way round.

This:

node (*children)[2];

reads as the following: the contents of children, when dereferenced (* is applied to them), yield something on which the array operator [] can be applied, yielding a node. Which means that children is a pointer to an array of two node. However, you do not want a pointer to an array, you want an array of two pointers. For that, you should put the parentheses thus:

node *(children[2]);

which then reads as: the contents of children are an array of two values; applying * on one of them yields a node. This is what you want: an array of two pointers to node. Note that this is equivalent to:

node *children[2];

because in the C syntax, when it comes to type expressions, the [] operator takes precedence over the * operator.

In your initial version, the C compiler grumbles because at the time it reads the declaration, it does not know what size node is (the structure definition is not complete yet), and, as such, has trouble imagining a type "array of two node". This is a rather indirect symptom of the problem.

Side note: you should refrain from using _node or any other identifier beginning with an underscore in your code. These identifiers are reserved for "the implementation". In practice, system headers may use them, and collisions could prove harmful. Personally, I tend to add the underscore at the end, like this: typedef struct node_ node;

这篇关于GCC:数组类型具有不完整的元素类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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