Thread: Nested Structure help ~

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    31

    Nested Structure help ~

    First, lets view the question...
    (i) Declare a tagged structure called DATE which consists of the following members:
    month (eg. 12)
    day (eg. 5)
    year (eg. 2001)
    (ii) Using typedef, declare a structure called STUDENT with the following members:
    StudentName (eg. Wilson Tan Wing Soon)

    ICNo (eg. 890225-06-0025)
    BirthDate (eg. as in (i) shown above)
    __________________________________________________ _____
    My coding so far....

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct DATE {
    
        int month;
    
        int day;
    
        int year;
    
    }
    
    typedef struct{
    
      char name[30];
    
      char ic[30];
    
      DATE date;
    
      } STUDENT;
    
    
    
    int main(void)
    
    {
    
        return 0;
    
    }


    __________________________________________________

    Help me ~ what mean by (ii) BirthDate? It say shown above?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Siaw View Post
    Help me ~ what mean by (ii) BirthDate? It say shown above?
    Code:
    typedef struct{
      char name[30];
      char ic[30];
      DATE birthdate;
      } STUDENT;

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    31
    Oh...I understand but now got an error...
    Error below
    __________________________________________________ _______
    C:\Documents and Settings\Administrator\Desktop\Q5.c|20|error: expected specifier-qualifier-list before 'DATE'|
    C:\Documents and Settings\Administrator\Desktop\Q5.c|22|error: two or more data types in declaration specifiers|
    ||=== Build finished: 2 errors, 0 warnings ===|
    __________________________________________________ ___

    How can I solve this error?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    typedef struct{
      char name[30];
      char ic[30];
      struct DATE birthdate;
      } STUDENT;

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    31
    Very thanks you. But now its still remains one error...

    C:\Documents and Settings\Administrator\Desktop\Q5.c|22|error: two or more data types in declaration specifiers|
    ||=== Build finished: 1 errors, 0 warnings ===|

    __________________________________________________ ___

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Read the error message... it's telling you there's an error on line 22 of your code (which I obviously cannot see)...
    So you need to look at that line and fix it.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    31
    Hmm...I try to figure it out but I still don't know what wrong...so I think I post my codes.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct DATE {
    
        int month;
    
        int day;
    
        int year;
    
    }
    
    typedef struct{
    
      char name[30];
    
      char ic[30];
    
      struct DATE birthdate;
    
      } STUDENT;
    
    
    
    int main(void)
    
    {
    
    
    
    
        return 0;
    
    }

  8. #8
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Code:
    struct DATE { 
        int month; 
        int day; 
        int year; 
    }; //<----------------------
    *cough*

    It gets me every time, too.
    Consider this post signed

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    31
    Lol...I never thinks is this problem....very thanks

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by bernt View Post
    Code:
    struct DATE { 
        int month; 
        int day; 
        int year; 
    }; //<----------------------
    *cough*

    It gets me every time, too.
    You too? I really wish I had a dollar for every time I've done that to myself... and I even missed it in his code... my bad.

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    31
    First, lets view the question...
    (i) Declare a tagged structure called DATE which consists of the following members:
    month (eg. 12)
    day (eg. 5)
    year (eg. 2001)

    (ii) Using typedef, declare a structure called STUDENT with the following members:
    StudentName (eg. Wilson Tan Wing Soon)
    ICNo (eg. 890225-06-0025)
    BirthDate (eg. as in (i) shown above)

    (iii) Using typedef, declare a structure called COURSE which is used to store course data. The structure must contain the following details:
    CourseTitle (eg. Diploma in Computer System Engineering)
    CommencingDate (eg. as in (i) above)
    StudentData (eg. as in (ii) above, there are 10 of these)

    (iv) Define a variable called IBcourse of the type COURSE which was declared in (iii).

    __________________________________________________ _______
    My coding below....

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct DATE {
    
        int month;
    
        int day;
    
        int year;
    
    } DATE;
    
    typedef struct{
    
      char name[30];
    
      char ic[30];
    
      struct DATE birthdate;
    
      } STUDENT;
    
      typedef struct {
    
        char course[30];
    
        struct DATE commencingdate;
    
        struct STUDENT studentdata;
    
      } COURSE;
    
    
    
    int main(void)
    
    {
    
    
    
    
        return 0;
    
    }


    __________________________________________________ ___
    So far, I have done until (iii). Now I have been stuck on 1 error.
    C:\Documents and Settings\Administrator\Desktop\Q5.c|30|error: field 'studentdata' has incomplete type|
    ||=== Build finished: 1 errors, 0 warnings ===|
    __________________________________________________ _____
    Please help me solve this error please ~ And also how can I done (iv)? Very thanks you ~

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct DATE {
     
        int month;
     
        int day;
     
        int year;
     
    } DATE;
    This isn't a typedef. It simply defines a structure of type 'struct DATE', and then declares a variable of that type called 'DATE'. To make additional instances of this type, you need to use 'struct DATE' everywhere you want one.
    Code:
    typedef struct{
     
      char name[30];
     
      char ic[30];
     
      struct DATE birthdate;
     
      } STUDENT;
    This does use a typedef, making a new type called 'STUDENT'. Every time you want an instance of this type, you now just need to write 'STUDENT varname;'
    Code:
      typedef struct {
     
        char course[30];
     
        struct DATE commencingdate; // that is correct
     
        struct STUDENT studentdata; // this is not
     
      } COURSE;

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Oct 2011
    Posts
    31
    Oh,...I understand but how can I do number (iv)? What does (iv) mean?

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It means to make an instance of COURSE that is called IBcourse. Just what it says:
    Quote Originally Posted by you
    Define a variable called IBcourse of the type COURSE which was declared in (iii).

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Oct 2011
    Posts
    31
    Ok...below is the code. Am I do right for (iv)?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
        struct DATE {
    
            int month;
    
            int day;
    
            int year;
    
    } DATE;
    
        typedef struct{
    
            char name[30];
    
            char ic[30];
    
            struct DATE birthdate;
    
      } STUDENT;
    
      typedef struct {
    
            char course[30];
    
            struct DATE commencingdate; // struct need struct
    
            STUDENT studentdata; // typedef no need struct
    
      } COURSE;
    
    
      int main(void)
    
        {
            COURSE IBcourse;
    
    
    
    
            return 0;
    
        }
    __________________________________________________ ____

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing and using a Array nested Structure
    By Jaxtomtom89 in forum C Programming
    Replies: 2
    Last Post: 11-30-2010, 02:50 AM
  2. pointer to nested structure
    By wahid in forum C Programming
    Replies: 2
    Last Post: 11-10-2010, 04:05 AM
  3. Displaying Data from a Nested Structure
    By shazg2000 in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2005, 10:16 AM
  4. initializing nested structure arrays
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 06-10-2004, 10:58 PM
  5. nested structure help
    By whistler in forum C Programming
    Replies: 1
    Last Post: 05-17-2002, 10:48 AM