类型定义,结构标签和别名 [英] typedefs, struct tag, and alias

查看:71
本文介绍了类型定义,结构标签和别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的C code片断:

Consider the following C code snippet:

typedef struct node{
    int val;
    struct node* left;
    struct node* right;
}node;

void inorderTraversal(node *p){
    inorderTraversal(p->left);
    printf("%d",p->val);
    inorderTraversal(p->right);
}

如果我只写 typedef结构而不是 typedef结构节点,我得到一个警告说通过兼容的指针类型的参数当我打电话inorderTravesal(根)在主要()。为什么即使程序不显示任何的错误,我得到这样的警告?

If I write only typedef struct instead of typedef struct node, I get a warning saying "passing argument of incompatible pointer type" when I call inorderTravesal(root) in main(). Why do I get this warning even though the program doesn't show any error?

推荐答案

如果你不给一个标签名的结构,

If you don't give a tag name to the struct,

struct node* left;

在结构定义声明了一个新的(不完全)键入结构节点。所以,当你传递一个指针(不完全)型,其中节点* 的预期,你传递一个不兼容的类型的指针。

in the struct definition declares a new (incomplete) type struct node. So when you pass a pointer to that (incomplete) type, where a node* is expected, you're passing a pointer of an incompatible type.

当你定义结构有成员都指向同一类型的,你必须能够命名的定义,类型,所以该类型必须是在范围内。

When the struct you define has members that are pointers to the same type, you must be able to name that type in the definition, so the type must be in scope.

如果你给结构的名称,类型是 - 从这一点上 - 在范围和可参照,但还没有完成,如结构节点(如果该标记是节点)。该类型定义完成后,该类型就可以被称为要么结构节点节点,无论你preFER。

If you give the struct a name, the type is - from that point on - in scope and can be referred to, although not yet complete, as struct node (if the tag is node). After the typedef is complete, the type can then be referred to as either struct node or node, whichever you prefer.

但是,如果你不给结构标记,类型是匿名的,直到类型定义是完整的,不能以任何方式之前被称为。而且,由于该线时

But if you don't give the struct a tag, the type is anonymous until the typedef is complete, and cannot in any way be referred to before that. And since when the line

struct node *left;

遇到,没有类型结构节点指的是已知的,该行声明了一个新的类型,结构节点,其中一无所知。编译器没有理由的类型与当前定义的连接。因此,在这一点上,<​​code>结构包含一个成员是一个指向一个未知的不完全类型。现在,在 inorderTraversal ,当你调用

is encountered, no type that struct node refers to is known, that line declares a new type, struct node, of which nothing is known. The compiler has no reason to connect that type with the one currently being defined. So at that point, the struct contains a member that is a pointer to an unknown incomplete type. Now, in inorderTraversal, when you call

inorderTraversal(p->left);

节点* P ,由节点定义 P-&GT;左是一个指向未知的不完全类型结构节点。如果 P 已经建立,使 P-&GT;左实际上是一个指向节点,事情仍将正常工作(除了可能对平台的地方指向不同的类型有不同的重新presentations),但你传递一个指向一种类型,其中一个指向一个不同类型的预计。由于一类是不完整的,这是与预期的类型不兼容。

with node *p, by the definition of node, p->left is a pointer to the unknown incomplete type struct node. If p has been created so that p->left is actually a pointer to node, things will work nevertheless (except possibly on platforms where pointers to different types have different representations), but you're passing a pointer to one type where a pointer to a different type is expected. Since the one type is incomplete, it is incompatible with the expected type.

这篇关于类型定义,结构标签和别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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