Thread: Nested Structures

  1. #1
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140

    Question Nested Structures

    How do you create a nested structure? is it anything along the lines of:

    struct info{
    char name[21];
    char address[31];
    struct dob;
    }INFO;

    struct dob{
    int day[3];
    int month[3];
    int year[5];
    }DOB;

    Then to write to the D.o.b struct something along the lines of:

    scanf("%d", INFO.DOB.day);

    Thanks for the help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, about like that

    Code:
    struct dob{ 
      int day; 
      int month; 
      int year; 
    }; 
    
    struct info{ 
      char name[21]; 
      char address[31]; 
      struct dob; 
    }INFO; 
    
    scanf("%d", &INFO.dob.day);

  3. #3
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140

    Talking

    Makes sense...

    Thanks!


  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Salem
    Yes, about like that
    Code:
    struct info{ 
      char name[21]; 
      char address[31]; 
      struct dob; 
    }INFO; 
    
    scanf("%d", &INFO.dob.day);
    Do you see anything odd about this line Salem?

    I thought variables had to have a name in addition to a type? Hehe.

    Now you COULD do it this way:
    Code:
    struct info {
        char name[21]; 
        char address[31]; 
        struct {
            int day;
            int month;
            int year;
        } dob;
    };
    Quzah.
    Last edited by quzah; 06-14-2002 at 04:42 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    14
    dob has to be defined first than info

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, 10:53 AM
  3. Nested Structures
    By Thantos in forum C Programming
    Replies: 2
    Last Post: 12-07-2003, 03:34 PM
  4. Nested structures
    By Supar in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2003, 09:27 PM
  5. Nested structures
    By Garfield in forum C Programming
    Replies: 8
    Last Post: 10-08-2001, 12:11 PM