Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
500 views
in Technique[技术] by (71.8m points)

c - typedef a struct before it's declared

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;
};

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?

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

--- >8 ---

Thank you all guys for so much helpful information.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...