Thread: Why create structs this way?

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    56

    Why create structs this way?

    So i been studying C, and got to structs and saw this example online:

    Code:
    #define MAX_DATA 512
    #define MAX_ROWS 100
    
    struct Address {
    int id;
    int set;
    char name[MAX_DATA];
    char email[MAX_DATA];
    };
    
    struct Database {
    struct Address rows[MAX_ROWS];
    };
    
    struct Connection {
    FILE *file;
    struct Database *db;
    };
    My question is why do people create structs that contain others?

    I understand Address, now Database it has inside struct Address rows[MAX-ROWS], this means that its creating an array that contains id,set,name,email and this values are stored in the array in that way

    id | set | name | email < ROW#1 (and it has a max of 100 rows)

    Ten Connection includes this array(rows) and a file name

    correct??

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Database includes an array of struct Address.The size of the array is MAX_ROWS.So every cell of the array has a struct Address stored in it,thus every cell contains an id,set,name and email.So DataBase contains an array of MAX_ROWS Address(es).
    Connection has a pointer to FILE and a pointer to struct Database

    Maybe this type of coding was elegant for these example

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It should be aligned with how you are looking at the data you're dealing with. Take a student for example. Each student will have classes, a GPA, a major, a student number for an ID, a first and last name (at least), etc. So it's natural to include all the above data, with the student struct.

    How you intuitively think about the data, is a good place to start with organizing structs, and structs inside other structs, too.

    Try to keep it simple, though. Did you see your error in the above code description? It's so easy to change MAX_ROWS, into MAX-ROWS, etc.

    Be sure you're clear about the difference between a prototype for a struct, and creating an instance of the struct.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    How would u call a variable??
    Rows[1].id = 6;

    ???

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    nvm i kinda got it...
    Code:
    struct Connection *p, test;
    struct Database db;
    p = &test;    
    p->db = &db;    
    p->db->rows[10].id = 10;     
    printf("%d\n",p->db->rows[10].id);
    Last edited by SuperMiguel; 06-29-2012 at 10:18 PM.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Make sure to understand how to 'travel' through different structs. It is used very frequently

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Typedef Structs inside Typdef structs
    By gremory in forum C Programming
    Replies: 21
    Last Post: 12-30-2011, 07:48 PM
  2. create and populate create bidimensional array
    By darkducke in forum C Programming
    Replies: 0
    Last Post: 12-03-2010, 07:06 AM
  3. Passing Structs Into An Array Of Structs.
    By TheTaoOfBill in forum C Programming
    Replies: 3
    Last Post: 10-07-2010, 09:38 AM
  4. Possible to create methods for data in structs?
    By eccles in forum C Programming
    Replies: 19
    Last Post: 12-15-2004, 09:46 AM
  5. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 07:14 PM