Declaring structures inside structures ?

This is a discussion on Declaring structures inside structures ? within the C Programming forums, part of the General Programming Boards category; How do you do it? plz and thank you....

  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
    32,854
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    ... 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, 03: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, 03: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-06-2006, 12:06 AM
  5. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 06:31 PM

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