Getting Started With C

Nested structures in c is a structure within structure.We can declare one structure inside the another structure in the same way structure members also declared inside structure.

Example:-
          1)For example two structures one is College (outer structure)another is student(inner strcuture).
          2)College has some data members like college_id,college_code,college_name.
          3)Student has some data members like student_pin,student_name,student_age etc..
          4)The student structure is nested within the college structure 

          Struct College
{
	Data_member1;
	Data_member2;
	Struct Student
	{
		Data_member1;
		Data_member2;
};
};

Syntax for accessing the data members of nested structure:
Outer_structure_var_name.Inner_structure_var_name.data_member

          
  • Examples :

    Program.c


  • Output:

            Enter name: Cprogram
            Enter age: 34
            Enter street: Belllabaratories
            Enter city: america
            Enter state: america
            
            Person Information:
            Name: Cprogram
            Age: 34
            Address: Belllabaratories, america, america
        
        

    Resources :

    Test Your Knowledge
    Choose The

    With supporting text below as a natural lead-in to additional content.

    Q1. Consider the following nested structures in C: struct Address { char street[50]; char city[30]; }; struct Person { char name[50]; int age; struct Address residence; }; How do you declare a variable named individual of type struct Person?

    Q2. If you want to access the city member of the residence structure for the individual variable, what is the correct syntax?