Hierarchical structures

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • momotaro
    Contributor
    • Sep 2006
    • 357

    Hierarchical structures

    those are my structers:
    [CODE=c]
    typedef struct _stud student;
    typedef struct _grades grades;
    struct _stud
    {
    char name[MAXSIZE];
    grades gr;
    };

    struct _grades
    {
    double final;
    char gpa;
    };
    [/CODE]

    and this is the line where the code crach:

    [CODE=c]
    scanf("%lf", &s->gr.final);
    [/CODE]
    And those are the errors:
    error C2079: 'gr' uses undefined struct '_grades'
    error C2224: left of '.final' must have struct/union type
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Move lines 9-13 before line 3 because that _grades struct isn' defined yet when
    the _stud struct is defined.

    kind regards,

    Jos

    Comment

    • momotaro
      Contributor
      • Sep 2006
      • 357

      #3
      but i thougt line 1 & 2 normaly solve this kind of problems???

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by momotaro
        but i thougt line 1 & 2 normaly solve this kind of problems???
        No they don't; typedefs just introduce a new name for another type; if the type
        is an imcomplete type (in your case) just a new name for an incomplete type
        is introduced by those typedefs.

        kind regards,

        Jos

        Comment

        • momotaro
          Contributor
          • Sep 2006
          • 357

          #5
          first question: could we do that kind of stuff?
          [CODE=c]
          typedef struct _stud student;
          typedef struct _grades grades;
          struct _stud
          {
          char name[MAXSIZE];
          grades *gr; // **********a pointer a another struct*********
          };

          struct _grades
          {
          double final;
          char gpa;
          };
          [/CODE]

          second question: and how to acces it
          is this correct:

          [CODE=c]
          //suppose that we have student s;
          scanf("%lf", &s->gr->final);
          [/CODE]

          third question:
          maybe we should allocate memory for *gr then..

          plz help

          Comment

          • ilikepython
            Recognized Expert Contributor
            • Feb 2007
            • 844

            #6
            Originally posted by momotaro
            first question: could we do that kind of stuff?
            [CODE=c]
            typedef struct _stud student;
            typedef struct _grades grades;
            struct _stud
            {
            char name[MAXSIZE];
            grades *gr; // **********a pointer a another struct*********
            };

            struct _grades
            {
            double final;
            char gpa;
            };
            [/CODE]

            second question: and how to acces it
            is this correct:

            [CODE=c]
            //suppose that we have student s;
            scanf("%lf", &s->gr->final);
            [/CODE]

            third question:
            maybe we should allocate memory for *gr then..

            plz help
            1)
            Yes you can.
            2)
            If s is a pointer then that should work.
            3)
            You have to otherwise you will get a segmentation fault.
            [code=c]
            s->gr = (grades *)malloc(sizeof (grades));
            [/code]

            Comment

            Working...