Nested structures

This is a discussion on Nested structures within the C Programming forums, part of the General Programming Boards category; I'm not sure about this one "aspect", if you may. Would it be better to do this: Code: struct student ...

  1. #1
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584

    Nested structures

    I'm not sure about this one "aspect", if you may. Would it be better to do this:
    Code:
    struct student {
        char name[50];
        struct student *next_stud;
        struct grade {
            int grade;
            struct grade *next_grad;
        };
    };
    and have nested structures, or better to do this:
    Code:
    struct grade {
        int grade;
        struct grade *next_grad;
    };
    
    struct student {
        char name[50];
        struct student *next_stud;
        struct grade *grad;
    };
    I don't think there is much of a difference, is there? I just didn't nest the structures the second time. Thanks.

    --Garfield
    1978 Silver Anniversary Corvette

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    The first one should be
    Code:
    struct student {
        char name[50];
        struct student *next_stud;
        struct grade {
            int grade;
            struct grade *next_grad;
        } *grad;
    };
    I think they're the same.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Oh, thanks. I forgot that I did have to "name", if you may, the grade struct. Thanks!

    --Garfield
    1978 Silver Anniversary Corvette

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    tha difference is "Scope" if you need the grade struct globaly then you will not be able to use it if it is nested while if it is not nested it can be used anywhere.

    so if you need a struct for only a single use in another struct then you could but don't have to, nest it in that struct.

    BTW: i may be quoting C++ scope rules but i don't think so.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > tha difference is "Scope" if you need the grade struct globaly then you will not be able to use it if it is nested while if it is not nested it can be used anywhere.
    That's exactly what I thought, until I tried it, but it didn't seem to be the case.

    I tried this with
    gcc version 2.8.1
    Borland C++ 5.3 for Win32
    and both saw the nested structure at the global scope.

    However, compiling it as C++ does draw a scope diagnostic.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    >
    I tried this with
    gcc version 2.8.1
    Borland C++ 5.3 for Win32
    and both saw the nested structure at the global scope
    <

    guess i am spoutin C++ scope rules, i tried and get the same.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  7. #7
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    But is that scope of the nested structure actually possible to be global? This example:
    Code:
    struct student {
        char name[50];
        struct student *next_stud;
        struct grade {
            int grade;
            struct grade *next_grad;
        } *grad;
    };
    You guys stated that struct grade (grad) actually has the global scope. So, then does char array 'name' have a global scope? Shouldn't it follow the same scope rules?

    --Garfield
    1978 Silver Anniversary Corvette

  8. #8
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    >
    You guys stated that struct grade (grad) actually has the global scope. So, then does char array 'name' have a global scope? Shouldn't it follow the same scope rules?
    <

    to the first question no, 'name' does not have global scope.

    to the second, it would be more likely that the struct should follow the same scope rules as the other data in order to maintain data hiding.

    but since the struct 'grade' is not technically a data member of struct 'student' and does not require data hiding,it is seen as global.
    at least thats my guess.

  9. #9
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Okay, I agree.
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested Structures Possible?
    By thetinman in forum C++ Programming
    Replies: 6
    Last Post: 09-05-2007, 11:22 AM
  2. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 09:53 AM
  3. Nested Structures
    By Thantos in forum C Programming
    Replies: 2
    Last Post: 12-07-2003, 02:34 PM
  4. Nested structures
    By Supar in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2003, 08:27 PM
  5. Nested Structures
    By Inept Pig in forum C Programming
    Replies: 4
    Last Post: 06-14-2002, 08:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21