Struct Initialization and Printing in C
I'm working on a problem involving the code below, and I'm a bit lost.
What is "class" doing, and how does that change how I would print the members? I've never seen an initialization like this before.
int main(){
struct Student {
char Initials2[2];
int id;
struct Student *teammate;
};
typedef struct Student SType;
#define XYpt &class[0]
#define ABpt &class[1]
#define RSpt &class[2]
#define CDpt &class[3]
#define JVpt &class[4]
#define RYpt &class[5]
SType class[6] = {
{{'X','Y'},123, RSpt},
{{'A','B'},23, RYpt},
{{'R','S'},11, XYpt},
{{'C','D'},44, JVpt},
{{'J','V'},42, CDpt},
{{'R','Y'},457, ABpt}
};
return 0;
}
c struct initialization
add a comment |
I'm working on a problem involving the code below, and I'm a bit lost.
What is "class" doing, and how does that change how I would print the members? I've never seen an initialization like this before.
int main(){
struct Student {
char Initials2[2];
int id;
struct Student *teammate;
};
typedef struct Student SType;
#define XYpt &class[0]
#define ABpt &class[1]
#define RSpt &class[2]
#define CDpt &class[3]
#define JVpt &class[4]
#define RYpt &class[5]
SType class[6] = {
{{'X','Y'},123, RSpt},
{{'A','B'},23, RYpt},
{{'R','S'},11, XYpt},
{{'C','D'},44, JVpt},
{{'J','V'},42, CDpt},
{{'R','Y'},457, ABpt}
};
return 0;
}
c struct initialization
1
This is an array definition with initializer.int x[6] = { 1, 2, 3, 4, 5, 6 };
means thatx
is an array of 6int
s and the members of the braced list are initializers for each member of the array
– M.M
Nov 26 '18 at 0:48
add a comment |
I'm working on a problem involving the code below, and I'm a bit lost.
What is "class" doing, and how does that change how I would print the members? I've never seen an initialization like this before.
int main(){
struct Student {
char Initials2[2];
int id;
struct Student *teammate;
};
typedef struct Student SType;
#define XYpt &class[0]
#define ABpt &class[1]
#define RSpt &class[2]
#define CDpt &class[3]
#define JVpt &class[4]
#define RYpt &class[5]
SType class[6] = {
{{'X','Y'},123, RSpt},
{{'A','B'},23, RYpt},
{{'R','S'},11, XYpt},
{{'C','D'},44, JVpt},
{{'J','V'},42, CDpt},
{{'R','Y'},457, ABpt}
};
return 0;
}
c struct initialization
I'm working on a problem involving the code below, and I'm a bit lost.
What is "class" doing, and how does that change how I would print the members? I've never seen an initialization like this before.
int main(){
struct Student {
char Initials2[2];
int id;
struct Student *teammate;
};
typedef struct Student SType;
#define XYpt &class[0]
#define ABpt &class[1]
#define RSpt &class[2]
#define CDpt &class[3]
#define JVpt &class[4]
#define RYpt &class[5]
SType class[6] = {
{{'X','Y'},123, RSpt},
{{'A','B'},23, RYpt},
{{'R','S'},11, XYpt},
{{'C','D'},44, JVpt},
{{'J','V'},42, CDpt},
{{'R','Y'},457, ABpt}
};
return 0;
}
c struct initialization
c struct initialization
edited Nov 26 '18 at 15:05
Lundin
112k17163271
112k17163271
asked Nov 25 '18 at 22:49
LokiLoki
1
1
1
This is an array definition with initializer.int x[6] = { 1, 2, 3, 4, 5, 6 };
means thatx
is an array of 6int
s and the members of the braced list are initializers for each member of the array
– M.M
Nov 26 '18 at 0:48
add a comment |
1
This is an array definition with initializer.int x[6] = { 1, 2, 3, 4, 5, 6 };
means thatx
is an array of 6int
s and the members of the braced list are initializers for each member of the array
– M.M
Nov 26 '18 at 0:48
1
1
This is an array definition with initializer.
int x[6] = { 1, 2, 3, 4, 5, 6 };
means that x
is an array of 6 int
s and the members of the braced list are initializers for each member of the array– M.M
Nov 26 '18 at 0:48
This is an array definition with initializer.
int x[6] = { 1, 2, 3, 4, 5, 6 };
means that x
is an array of 6 int
s and the members of the braced list are initializers for each member of the array– M.M
Nov 26 '18 at 0:48
add a comment |
1 Answer
1
active
oldest
votes
What the code does:
struct Student
is a bit special, as it is containing a pointer to an object of the same typestruct Student *teammate
. This is possible by using a pointer to an object with the "struct tag"Student
, which acts as a form of forward declaration.
typedef struct Student SType;
just hides away thestruct
keyword, which is a coding style matter. It would have been cleaner to write the whole thing like this:
typedef struct Student {
char Initials2[2];
int id;
struct Student *teammate;
} SType;
SType class[6] = { {{'X','Y'},123, RSpt}, ....
is just an array of 6 structs, each initialized. The bunch of macros expand to variable addresses of the same array named "class". This is poor style - the programmer used this as a dirty way to "name" each item in the array. The postfix "pt" seems to mean pointer.
How the code could have been written:
Rather than using ugly macros, it is possible to associate each item of an array with an identifier, by using a union
. For example:
typedef union
{
struct foo
{
int foo;
int bar;
} foo;
int array [2];
} foobar;
Here, an object foobar fb;
can be accessed as fb.foo.foo
or fb.array[0]
and it means the same item 0 of the array. With modern standard C, we can drop the inner struct name (anonymous struct) and just access the objects as fb.foo
.
Also, this can be combined with designated initializers to initialized certain named members of the struct by their name: foobar fb { .foo = 1, .bar = 2 };
.
Rewriting your example by using unions, anonymous struct and designated initializers, we get this instead:
typedef struct student {
char initials [2];
int id;
struct student *teammate;
} s_type;
typedef union
{
struct
{
s_type XY;
s_type AB;
s_type RS;
s_type CD;
s_type JV;
s_type RY;
};
s_type array [6];
} class_t;
class_t class =
{
.XY = { .initials={'X','Y'}, .id=123, .teammate = &class.RS},
.AB = { .initials={'A','B'}, .id= 23, .teammate = &class.RY},
.RS = { .initials={'R','S'}, .id= 11, .teammate = &class.XY},
.CD = { .initials={'C','D'}, .id= 44, .teammate = &class.JV},
.JV = { .initials={'J','V'}, .id= 42, .teammate = &class.CD},
.RY = { .initials={'R','Y'}, .id=457, .teammate = &class.AB},
};
This is much easier to read and understand. Plus we can still use it as an array with class.array[i]
if we want.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472786%2fstruct-initialization-and-printing-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
What the code does:
struct Student
is a bit special, as it is containing a pointer to an object of the same typestruct Student *teammate
. This is possible by using a pointer to an object with the "struct tag"Student
, which acts as a form of forward declaration.
typedef struct Student SType;
just hides away thestruct
keyword, which is a coding style matter. It would have been cleaner to write the whole thing like this:
typedef struct Student {
char Initials2[2];
int id;
struct Student *teammate;
} SType;
SType class[6] = { {{'X','Y'},123, RSpt}, ....
is just an array of 6 structs, each initialized. The bunch of macros expand to variable addresses of the same array named "class". This is poor style - the programmer used this as a dirty way to "name" each item in the array. The postfix "pt" seems to mean pointer.
How the code could have been written:
Rather than using ugly macros, it is possible to associate each item of an array with an identifier, by using a union
. For example:
typedef union
{
struct foo
{
int foo;
int bar;
} foo;
int array [2];
} foobar;
Here, an object foobar fb;
can be accessed as fb.foo.foo
or fb.array[0]
and it means the same item 0 of the array. With modern standard C, we can drop the inner struct name (anonymous struct) and just access the objects as fb.foo
.
Also, this can be combined with designated initializers to initialized certain named members of the struct by their name: foobar fb { .foo = 1, .bar = 2 };
.
Rewriting your example by using unions, anonymous struct and designated initializers, we get this instead:
typedef struct student {
char initials [2];
int id;
struct student *teammate;
} s_type;
typedef union
{
struct
{
s_type XY;
s_type AB;
s_type RS;
s_type CD;
s_type JV;
s_type RY;
};
s_type array [6];
} class_t;
class_t class =
{
.XY = { .initials={'X','Y'}, .id=123, .teammate = &class.RS},
.AB = { .initials={'A','B'}, .id= 23, .teammate = &class.RY},
.RS = { .initials={'R','S'}, .id= 11, .teammate = &class.XY},
.CD = { .initials={'C','D'}, .id= 44, .teammate = &class.JV},
.JV = { .initials={'J','V'}, .id= 42, .teammate = &class.CD},
.RY = { .initials={'R','Y'}, .id=457, .teammate = &class.AB},
};
This is much easier to read and understand. Plus we can still use it as an array with class.array[i]
if we want.
add a comment |
What the code does:
struct Student
is a bit special, as it is containing a pointer to an object of the same typestruct Student *teammate
. This is possible by using a pointer to an object with the "struct tag"Student
, which acts as a form of forward declaration.
typedef struct Student SType;
just hides away thestruct
keyword, which is a coding style matter. It would have been cleaner to write the whole thing like this:
typedef struct Student {
char Initials2[2];
int id;
struct Student *teammate;
} SType;
SType class[6] = { {{'X','Y'},123, RSpt}, ....
is just an array of 6 structs, each initialized. The bunch of macros expand to variable addresses of the same array named "class". This is poor style - the programmer used this as a dirty way to "name" each item in the array. The postfix "pt" seems to mean pointer.
How the code could have been written:
Rather than using ugly macros, it is possible to associate each item of an array with an identifier, by using a union
. For example:
typedef union
{
struct foo
{
int foo;
int bar;
} foo;
int array [2];
} foobar;
Here, an object foobar fb;
can be accessed as fb.foo.foo
or fb.array[0]
and it means the same item 0 of the array. With modern standard C, we can drop the inner struct name (anonymous struct) and just access the objects as fb.foo
.
Also, this can be combined with designated initializers to initialized certain named members of the struct by their name: foobar fb { .foo = 1, .bar = 2 };
.
Rewriting your example by using unions, anonymous struct and designated initializers, we get this instead:
typedef struct student {
char initials [2];
int id;
struct student *teammate;
} s_type;
typedef union
{
struct
{
s_type XY;
s_type AB;
s_type RS;
s_type CD;
s_type JV;
s_type RY;
};
s_type array [6];
} class_t;
class_t class =
{
.XY = { .initials={'X','Y'}, .id=123, .teammate = &class.RS},
.AB = { .initials={'A','B'}, .id= 23, .teammate = &class.RY},
.RS = { .initials={'R','S'}, .id= 11, .teammate = &class.XY},
.CD = { .initials={'C','D'}, .id= 44, .teammate = &class.JV},
.JV = { .initials={'J','V'}, .id= 42, .teammate = &class.CD},
.RY = { .initials={'R','Y'}, .id=457, .teammate = &class.AB},
};
This is much easier to read and understand. Plus we can still use it as an array with class.array[i]
if we want.
add a comment |
What the code does:
struct Student
is a bit special, as it is containing a pointer to an object of the same typestruct Student *teammate
. This is possible by using a pointer to an object with the "struct tag"Student
, which acts as a form of forward declaration.
typedef struct Student SType;
just hides away thestruct
keyword, which is a coding style matter. It would have been cleaner to write the whole thing like this:
typedef struct Student {
char Initials2[2];
int id;
struct Student *teammate;
} SType;
SType class[6] = { {{'X','Y'},123, RSpt}, ....
is just an array of 6 structs, each initialized. The bunch of macros expand to variable addresses of the same array named "class". This is poor style - the programmer used this as a dirty way to "name" each item in the array. The postfix "pt" seems to mean pointer.
How the code could have been written:
Rather than using ugly macros, it is possible to associate each item of an array with an identifier, by using a union
. For example:
typedef union
{
struct foo
{
int foo;
int bar;
} foo;
int array [2];
} foobar;
Here, an object foobar fb;
can be accessed as fb.foo.foo
or fb.array[0]
and it means the same item 0 of the array. With modern standard C, we can drop the inner struct name (anonymous struct) and just access the objects as fb.foo
.
Also, this can be combined with designated initializers to initialized certain named members of the struct by their name: foobar fb { .foo = 1, .bar = 2 };
.
Rewriting your example by using unions, anonymous struct and designated initializers, we get this instead:
typedef struct student {
char initials [2];
int id;
struct student *teammate;
} s_type;
typedef union
{
struct
{
s_type XY;
s_type AB;
s_type RS;
s_type CD;
s_type JV;
s_type RY;
};
s_type array [6];
} class_t;
class_t class =
{
.XY = { .initials={'X','Y'}, .id=123, .teammate = &class.RS},
.AB = { .initials={'A','B'}, .id= 23, .teammate = &class.RY},
.RS = { .initials={'R','S'}, .id= 11, .teammate = &class.XY},
.CD = { .initials={'C','D'}, .id= 44, .teammate = &class.JV},
.JV = { .initials={'J','V'}, .id= 42, .teammate = &class.CD},
.RY = { .initials={'R','Y'}, .id=457, .teammate = &class.AB},
};
This is much easier to read and understand. Plus we can still use it as an array with class.array[i]
if we want.
What the code does:
struct Student
is a bit special, as it is containing a pointer to an object of the same typestruct Student *teammate
. This is possible by using a pointer to an object with the "struct tag"Student
, which acts as a form of forward declaration.
typedef struct Student SType;
just hides away thestruct
keyword, which is a coding style matter. It would have been cleaner to write the whole thing like this:
typedef struct Student {
char Initials2[2];
int id;
struct Student *teammate;
} SType;
SType class[6] = { {{'X','Y'},123, RSpt}, ....
is just an array of 6 structs, each initialized. The bunch of macros expand to variable addresses of the same array named "class". This is poor style - the programmer used this as a dirty way to "name" each item in the array. The postfix "pt" seems to mean pointer.
How the code could have been written:
Rather than using ugly macros, it is possible to associate each item of an array with an identifier, by using a union
. For example:
typedef union
{
struct foo
{
int foo;
int bar;
} foo;
int array [2];
} foobar;
Here, an object foobar fb;
can be accessed as fb.foo.foo
or fb.array[0]
and it means the same item 0 of the array. With modern standard C, we can drop the inner struct name (anonymous struct) and just access the objects as fb.foo
.
Also, this can be combined with designated initializers to initialized certain named members of the struct by their name: foobar fb { .foo = 1, .bar = 2 };
.
Rewriting your example by using unions, anonymous struct and designated initializers, we get this instead:
typedef struct student {
char initials [2];
int id;
struct student *teammate;
} s_type;
typedef union
{
struct
{
s_type XY;
s_type AB;
s_type RS;
s_type CD;
s_type JV;
s_type RY;
};
s_type array [6];
} class_t;
class_t class =
{
.XY = { .initials={'X','Y'}, .id=123, .teammate = &class.RS},
.AB = { .initials={'A','B'}, .id= 23, .teammate = &class.RY},
.RS = { .initials={'R','S'}, .id= 11, .teammate = &class.XY},
.CD = { .initials={'C','D'}, .id= 44, .teammate = &class.JV},
.JV = { .initials={'J','V'}, .id= 42, .teammate = &class.CD},
.RY = { .initials={'R','Y'}, .id=457, .teammate = &class.AB},
};
This is much easier to read and understand. Plus we can still use it as an array with class.array[i]
if we want.
edited Nov 26 '18 at 15:03
answered Nov 26 '18 at 14:58
LundinLundin
112k17163271
112k17163271
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472786%2fstruct-initialization-and-printing-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
This is an array definition with initializer.
int x[6] = { 1, 2, 3, 4, 5, 6 };
means thatx
is an array of 6int
s and the members of the braced list are initializers for each member of the array– M.M
Nov 26 '18 at 0:48