Struct Initialization and Printing in C












-1















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









share|improve this question




















  • 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 ints and the members of the braced list are initializers for each member of the array

    – M.M
    Nov 26 '18 at 0:48
















-1















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









share|improve this question




















  • 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 ints 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








-1








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









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 that x is an array of 6 ints and the members of the braced list are initializers for each member of the array

    – M.M
    Nov 26 '18 at 0:48














  • 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 ints 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 ints 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 ints and the members of the braced list are initializers for each member of the array

– M.M
Nov 26 '18 at 0:48












1 Answer
1






active

oldest

votes


















1














What the code does:




  • struct Student is a bit special, as it is containing a pointer to an object of the same type struct 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 the struct 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.






share|improve this answer

























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


    }
    });














    draft saved

    draft discarded


















    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









    1














    What the code does:




    • struct Student is a bit special, as it is containing a pointer to an object of the same type struct 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 the struct 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.






    share|improve this answer






























      1














      What the code does:




      • struct Student is a bit special, as it is containing a pointer to an object of the same type struct 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 the struct 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.






      share|improve this answer




























        1












        1








        1







        What the code does:




        • struct Student is a bit special, as it is containing a pointer to an object of the same type struct 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 the struct 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.






        share|improve this answer















        What the code does:




        • struct Student is a bit special, as it is containing a pointer to an object of the same type struct 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 the struct 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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 26 '18 at 15:03

























        answered Nov 26 '18 at 14:58









        LundinLundin

        112k17163271




        112k17163271
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            To store a contact into the json file from server.js file using a class in NodeJS

            Redirect URL with Chrome Remote Debugging Android Devices

            Dieringhausen