的typedef一个结构它的声明之前 [英] typedef a struct before it's declared

查看:125
本文介绍了的typedef一个结构它的声明之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是一个初学者,我很熟悉下面的语句:

I'm not a beginner, I'm very familiar with the following idiom:

typedef struct Foo_ Foo;// I know typedef struct Foo Foo is fine, I'm just trying to make it clearer
struct Foo_
{
    int value;
    Foo *link;
};

我只是突然感到困惑,因为我的理解是,它不能使用一个名称(标识),它的声明之前。但在声明 typedef结构Foo_美孚,标识符富_ 还不存在!为什么编译器允许这种情况发生?会有人请阐明这一些轻,给我解释一下什么是对这种语法的理由?

I'm just suddenly feel confused, because my understanding is that it's not allowed to use a name(identifier) before it's declared. But in the declaration typedef struct Foo_ Foo, the identifier Foo_ does not yet exist! How come the compiler permit this happen? Would anybody please shed some light on this, explain to me what's the justification for this kind of syntax?

维基百科报价:的目的的typedef 是分配选择的名称以现有各类

Wikipedia quote : The purpose of typedef is to assign alternative names to EXISTING types.

---> 8 ---

--- >8 ---

感谢您这么多有用的信息的所有球员。

Thank you all guys for so much helpful information.

推荐答案

这是完全没问题。第一次使用像你这样的结构标签是结构类型的向前声明。

This is completely fine. The first use of struct tag like yours is a forward declaration of the struct type.

但是要注意你的 _Foo 的用法是的不可以一致。与领先的下划线和下面的大写字母标识符被保留。不要那样做。尾随下划线将是确定的。

Beware though that your usage of _Foo is not conforming. Identifiers with leading underscore and following capital letter are reserved. Don't do that. Trailing underscore would be ok.

这篇关于的typedef一个结构它的声明之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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