Structures within structures

This is a discussion on Structures within structures within the C Programming forums, part of the General Programming Boards category; Hi, I have a specific problem. I have a structure within a structure; Code: struct class{ int tag; double num; ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    1

    Structures within structures

    Hi,
    I have a specific problem. I have a structure within a structure;

    Code:
    struct class{
    int tag;
    double num;
    double age;
    } info[200];
    
    struct strore{
    int num;
    double agemin;
    double agemax;
    struct class *pupil;
    }data[5][5];
    The first struct contains the details of some 200 persons. The second structure makes a cluster of those persons to a 5X5 array of "struct store" within a certain age group and stores them into the struct class pupil. I want to know how I can allocate dynamically memory to the "struct class *pupil" and access it's members.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Why allocate memory? You already have 200 "info" class things on the stack, just point to them.

    Also "class" is a C++ keyword so it's a very poor name for a variable / type / identifier.

    An example of what I'm talking about,

    Code:
    struct cls
    {
       int tag;
       double num;
       double age;
    } info[200];
    
    struct strore
    {
       int num;
       double agemin;
       double agemax;
       struct cls *pupil;
    }data[5][5];
    
    /* ... */
    
    data[0][0].pupil = &info[0];
    data[0][0].agemax = 5;
    /* etc */

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    I thought having the keyword class would generate an error message if he's going to use that.. specially if he's going to use a compiler that can compile C/C++.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,157
    Quote Originally Posted by $l4xklynx
    I thought having the keyword class would generate an error message if he's going to use that.. specially if he's going to use a compiler that can compile C/C++.
    If the code was being compiled as C then there would not be such an error.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    If you wanted to dynamically allocate memory you would do this:
    Code:
    struct class
    {
       int tag;
       double num;
       double age;
    } info[200];
    
    struct strore
    {
       int num;
       double agemin;
       double agemax;
       struct class *pupil;
    }data[5][5];
    
    /* ... */
    int i,j;
    for (i = 0; i < 5; ++i)
        for (i = 0; i < 5; ++i)
            data[i][j].pupil = malloc(SIZE_YOU_WANT * sizeof(data[i][j].pupil));
    
    //copy info[120] into data[0][0]
    data[0][0].pupil->tag = info[120].tag;
    data[0][0].pupil->num = info[120].num;
    data[0][0].pupil->age = info[120].age;
    Now each pupil (total 25) will hold an array of "struct class" of the size SIZE_YOU_WANT

    I am guessing you want SIZE_YOU_WANT to be 8 so you can split the infor[200] into a data[5][5]?

    Dunno if that's what you wanted

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by laserlight View Post
    If the code was being compiled as C then there would not be such an error.
    I see, but it's still not good to use because it's a C++ keyword right?

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > I see, but it's still not good to use because it's a C++ keyword right?
    Correct. It would prevent your C code from being compiled as C++. Thus it's a good idea to avoid all C++ keywords when you're writing in C.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,758
    Or a useful reminder that you can't just rename foo.c to foo.cpp without thinking about it.
    http://david.tribble.com/text/cdiffs.htm

    Just doing the rename and finding it still compiles doesn't mean your work is done.
    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.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    //copy info[120] into data[0][0]
    data[0][0].pupil->tag = info[120].tag;
    data[0][0].pupil->num = info[120].num;
    data[0][0].pupil->age = info[120].age;
    isn't this more easy on the eyes/brain:
    Code:
    //copy info[120] into data[0][0]
    data[0][0].pupil = info[120];
    Not to mention that if the "class" structure ever changes in content (e.g. a field is added or removed), there isn't any code to change - otherwise we could quite easily have a situation where the code has to be changed in lots of places just because one field is added, removed or renamed.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    In other words, you can assign structures to each other directly, without fiddling with the structure members. This is basically what happens when you pass a structure to a function.
    Code:
    void function(struct something it);
    
    struct something another;
    function(another);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. Structures within Structures
    By Misko82 in forum C Programming
    Replies: 2
    Last Post: 08-27-2007, 01:25 AM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-06-2006, 12:06 AM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 10:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 01:59 PM

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