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
371 views
in Technique[技术] by (71.8m points)

c - Why do we leave some structure variables out of the curly brackets?

I'm trying to understand struct in C. I couldn't get the idea about this definition below. Why do we leave aCard, deck[ ] and *cardPtr out? What is the difference between including them in and leaving them out?

 struct card {
    char *face;
    char *suit;
 } aCard, deck[52], *cardPtr;
question from:https://stackoverflow.com/questions/52851455/why-do-we-leave-some-structure-variables-out-of-the-curly-brackets

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

1 Reply

0 votes
by (71.8m points)

You are mixing things up. A struct card has the members face and suit. But there are three variables using the struct card type, namely aCard, deck, cardPtr.

Alternatively one could have written:

typedef struct {
    char *face;
    char *suit;
} Card;

Card aCard, deck[52], *cardPtr;
// or even
Card aCard;
Card deck[52];
Card *cardPtr;

For the typedef have a look at: Why should we typedef a struct so often in C? (It goes into the typedef struct { ... } Foo; vs struct Foo {...} debate).


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

...