Thread: Declaring structures inside structures ?

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    kingston jamaica
    Posts
    28

    Declaring structures inside structures ?

    How do you do it?

    plz and thank you.

  2. #2
    Registered User
    Join Date
    Apr 2010
    Location
    kingston jamaica
    Posts
    28
    anyone??????? i didn't realise that the question was that hard.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can nest structs, no problem:

    Code:
    struct NAME {
      char fname[20];
      char lname[20];
    }
    
    struct student {
      struct NAME;
      int ID;
      char major[30];
      char advisor[30];
      float GPA;
    }students[50];
    
    Access is via the dot operator: 
    for(i = 0; i < 50; i++)
      printf("%s", students[i].NAME.fname);
    //etc.

    In real code, you'd want just the definition of the structs global, and the declarations of the students array to be done in main(), probably.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Location
    kingston jamaica
    Posts
    28
    thank you.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're quite welcome, J.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > anyone??????? i didn't realise that the question was that hard.
    You only waited 15 minutes before deciding it was too hard.
    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.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    ... and at midnight.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures and dynamically allocated arrays
    By Bandizzle in forum C Programming
    Replies: 7
    Last Post: 10-04-2009, 02:05 PM
  2. Reading the file, and filling the array of structures
    By BlackOps in forum C Programming
    Replies: 13
    Last Post: 07-12-2009, 02:03 PM
  3. call to realloc() inside a function: memory problem
    By simone.marras in forum C Programming
    Replies: 15
    Last Post: 11-30-2008, 10:01 AM
  4. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  5. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 06:31 PM