Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...