Why do we leave some structure variables out of the curly brackets?
Clash Royale CLAN TAG#URR8PPP
up vote
8
down vote
favorite
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;
c
add a comment |Â
up vote
8
down vote
favorite
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;
c
Think of it this way: the deck, per example is not the property of a card. A card can exist on its own without a deck. So the deck is not part ofcard
.
– Blaze
23 mins ago
add a comment |Â
up vote
8
down vote
favorite
up vote
8
down vote
favorite
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;
c
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;
c
c
edited 16 mins ago
Micha Wiedenmann
9,0081061100
9,0081061100
asked 28 mins ago
Belverus
514
514
Think of it this way: the deck, per example is not the property of a card. A card can exist on its own without a deck. So the deck is not part ofcard
.
– Blaze
23 mins ago
add a comment |Â
Think of it this way: the deck, per example is not the property of a card. A card can exist on its own without a deck. So the deck is not part ofcard
.
– Blaze
23 mins ago
Think of it this way: the deck, per example is not the property of a card. A card can exist on its own without a deck. So the deck is not part of
card
.– Blaze
23 mins ago
Think of it this way: the deck, per example is not the property of a card. A card can exist on its own without a deck. So the deck is not part of
card
.– Blaze
23 mins ago
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
8
down vote
accepted
You are mixing things up. A struct card
has the fields face
and suit
. But there are three variables using the struct card
type, namely aCard, deck, cardPtr
.
Alternatively one could have written:
typdef 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 typedef struct ... Foo;
vs struct Foo ...
debate).
add a comment |Â
up vote
4
down vote
Your piece of code could be written like this which makes it clearer:
struct card // define the struct card
char *face;
char *suit;
;
struct card aCard; // declare a variable "aCard" of type "struct card "
struct card deck[52] // declare an array of 52 variables of type "struct card"
struct card *cardPtr; // declare a variable "cardPtr" of type "pointer to struct card"
add a comment |Â
up vote
2
down vote
a
struct card
char *face;
char *suit;
is just like int
, but it is user defined, and
aCard, deck[52], *cardPtr;
are variable names, e.g.
int aCard, deck[41], *cardPtr;
add a comment |Â
up vote
1
down vote
aCard
and the rest you have mentioned are variables of type struct card
and each of these variables contain char *face
and char *suit
. So how can some variable can contain itself i.e. aCard
being inside the struct node.
add a comment |Â
up vote
0
down vote
You have declared three variables. aCard
is of type struct card
, deck
is of type struct card[52]
and cardPtr
is of type struct card *
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
You are mixing things up. A struct card
has the fields face
and suit
. But there are three variables using the struct card
type, namely aCard, deck, cardPtr
.
Alternatively one could have written:
typdef 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 typedef struct ... Foo;
vs struct Foo ...
debate).
add a comment |Â
up vote
8
down vote
accepted
You are mixing things up. A struct card
has the fields face
and suit
. But there are three variables using the struct card
type, namely aCard, deck, cardPtr
.
Alternatively one could have written:
typdef 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 typedef struct ... Foo;
vs struct Foo ...
debate).
add a comment |Â
up vote
8
down vote
accepted
up vote
8
down vote
accepted
You are mixing things up. A struct card
has the fields face
and suit
. But there are three variables using the struct card
type, namely aCard, deck, cardPtr
.
Alternatively one could have written:
typdef 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 typedef struct ... Foo;
vs struct Foo ...
debate).
You are mixing things up. A struct card
has the fields face
and suit
. But there are three variables using the struct card
type, namely aCard, deck, cardPtr
.
Alternatively one could have written:
typdef 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 typedef struct ... Foo;
vs struct Foo ...
debate).
edited 18 mins ago
answered 26 mins ago
Micha Wiedenmann
9,0081061100
9,0081061100
add a comment |Â
add a comment |Â
up vote
4
down vote
Your piece of code could be written like this which makes it clearer:
struct card // define the struct card
char *face;
char *suit;
;
struct card aCard; // declare a variable "aCard" of type "struct card "
struct card deck[52] // declare an array of 52 variables of type "struct card"
struct card *cardPtr; // declare a variable "cardPtr" of type "pointer to struct card"
add a comment |Â
up vote
4
down vote
Your piece of code could be written like this which makes it clearer:
struct card // define the struct card
char *face;
char *suit;
;
struct card aCard; // declare a variable "aCard" of type "struct card "
struct card deck[52] // declare an array of 52 variables of type "struct card"
struct card *cardPtr; // declare a variable "cardPtr" of type "pointer to struct card"
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Your piece of code could be written like this which makes it clearer:
struct card // define the struct card
char *face;
char *suit;
;
struct card aCard; // declare a variable "aCard" of type "struct card "
struct card deck[52] // declare an array of 52 variables of type "struct card"
struct card *cardPtr; // declare a variable "cardPtr" of type "pointer to struct card"
Your piece of code could be written like this which makes it clearer:
struct card // define the struct card
char *face;
char *suit;
;
struct card aCard; // declare a variable "aCard" of type "struct card "
struct card deck[52] // declare an array of 52 variables of type "struct card"
struct card *cardPtr; // declare a variable "cardPtr" of type "pointer to struct card"
answered 21 mins ago
Jabberwocky
24.9k93666
24.9k93666
add a comment |Â
add a comment |Â
up vote
2
down vote
a
struct card
char *face;
char *suit;
is just like int
, but it is user defined, and
aCard, deck[52], *cardPtr;
are variable names, e.g.
int aCard, deck[41], *cardPtr;
add a comment |Â
up vote
2
down vote
a
struct card
char *face;
char *suit;
is just like int
, but it is user defined, and
aCard, deck[52], *cardPtr;
are variable names, e.g.
int aCard, deck[41], *cardPtr;
add a comment |Â
up vote
2
down vote
up vote
2
down vote
a
struct card
char *face;
char *suit;
is just like int
, but it is user defined, and
aCard, deck[52], *cardPtr;
are variable names, e.g.
int aCard, deck[41], *cardPtr;
a
struct card
char *face;
char *suit;
is just like int
, but it is user defined, and
aCard, deck[52], *cardPtr;
are variable names, e.g.
int aCard, deck[41], *cardPtr;
answered 21 mins ago
BladeMight
7861021
7861021
add a comment |Â
add a comment |Â
up vote
1
down vote
aCard
and the rest you have mentioned are variables of type struct card
and each of these variables contain char *face
and char *suit
. So how can some variable can contain itself i.e. aCard
being inside the struct node.
add a comment |Â
up vote
1
down vote
aCard
and the rest you have mentioned are variables of type struct card
and each of these variables contain char *face
and char *suit
. So how can some variable can contain itself i.e. aCard
being inside the struct node.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
aCard
and the rest you have mentioned are variables of type struct card
and each of these variables contain char *face
and char *suit
. So how can some variable can contain itself i.e. aCard
being inside the struct node.
aCard
and the rest you have mentioned are variables of type struct card
and each of these variables contain char *face
and char *suit
. So how can some variable can contain itself i.e. aCard
being inside the struct node.
answered 24 mins ago
Observer
956113
956113
add a comment |Â
add a comment |Â
up vote
0
down vote
You have declared three variables. aCard
is of type struct card
, deck
is of type struct card[52]
and cardPtr
is of type struct card *
add a comment |Â
up vote
0
down vote
You have declared three variables. aCard
is of type struct card
, deck
is of type struct card[52]
and cardPtr
is of type struct card *
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You have declared three variables. aCard
is of type struct card
, deck
is of type struct card[52]
and cardPtr
is of type struct card *
You have declared three variables. aCard
is of type struct card
, deck
is of type struct card[52]
and cardPtr
is of type struct card *
answered 22 mins ago


Broman
5,64092241
5,64092241
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52851455%2fwhy-do-we-leave-some-structure-variables-out-of-the-curly-brackets%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Think of it this way: the deck, per example is not the property of a card. A card can exist on its own without a deck. So the deck is not part of
card
.– Blaze
23 mins ago