Thread: Nested structure doubt

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    59

    Nested structure doubt

    This define structure whose name is student

    Code:
     struct student{   
        char name[30];     
        int rollNo;
        
        struct dateOfBirth *age;
        
        }DOB;    /*created structure varoable DOB*/
    };
    This structure has three member s
    1. Name is member of structure and it's type of char
    2. rollNo is member of structure and it's type of int
    3. dateOfBirth is member of structure and type of struct

    Does this declaration is equivalent to below declaration ?

    Code:
    struct student{
        char name[30];
        int rollNo;
        
        struct dateOfBirth {*age}; // is it valid line 
    
        }DOB;    /*created structure varoable DOB*/
    };

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,249
    Quote Originally Posted by vajra11
    This define structure whose name is student
    Your first struct declaration is not syntactically correct in the first place. Perhaps what you wanted to do was this:
    Code:
    struct student {
        char name[30];
        int rollNo;
        struct dateOfBirth *age;
    } DOB; /* created structure varoable DOB */
    The above declares a struct named struct student with three members:
    • name of type char[30]
    • rollNo of type int
    • age of type struct dateOfBirth*

    Additionally, the declaration also declares an object of type struct student named DOB. Note that there is no "nested structure" in your example: what you have is a member of a pointer to struct type.

    Quote Originally Posted by vajra11
    Does this declaration is equivalent to below declaration ?
    No, your second struct declaration is not syntactically correct either. The line that you mentioned is simply invalid syntax.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2018
    Posts
    59
    Quote Originally Posted by laserlight View Post
    Your first struct declaration is not syntactically correct in the first place.
    I missed bracket

    lets take an example of a structure
    Code:
    struct student {
        char name[30];   //name of type char[30]
       int rollNo;          //rollNo of type int
        struct dateOfBirth *age; //age of type struct dateOfBirth*
    } DOB; /* created structure varoable DOB */
    I don't understand red line in the structure
    Code:
    struct student {    
       char name[30];   //name of type char[30]
        int rollNo;         //rollNo of type int
    
        struct dateOfBirth *age; //age of type struct dateOfBirth*
    } DOB; /* created structure varoable DOB */
    Pointer is variable that hold the address of another variable

    We have declared structure datoFBirth using pointer

    What is use of this line struct dateOfBirth *age; ?

    I think it's structure member that is pointer to structure and age is pointer that store the address

    What;s the pointer pointing here
    Last edited by vajra11; 4 Weeks Ago at 11:19 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubt in pointer to structure
    By karthik537 in forum C Programming
    Replies: 6
    Last Post: 10-03-2012, 09:15 AM
  2. Basic doubt in C structure
    By cbalu in forum Linux Programming
    Replies: 2
    Last Post: 08-17-2009, 11:03 AM
  3. Doubt in structure
    By karthik537 in forum C Programming
    Replies: 15
    Last Post: 01-21-2009, 03:52 AM
  4. doubt regarding C structure in linux
    By Bargi in forum C Programming
    Replies: 2
    Last Post: 01-23-2007, 06:18 AM
  5. structure doubt...
    By incognito54 in forum C Programming
    Replies: 6
    Last Post: 04-21-2004, 11:54 AM

Tags for this Thread