When a structure is a member of another structure it is called a nested structure.

Struct address      \\ A
{
    plot char [30], struc char[30];
    city char[30]
}
struct student      \\ B
{
    name char[30];
    marks float;
    struct address adr;     \\ C
}
main ( )
{
    struct student student1; \\ D
    struct student class[20];      \\ E
    class[1].marks = 70;     \\ F
    class[1].name = " Anil ";
    class[1].adr.plot = "7 ";        \\ G
    class[1].adr.street = " Mg Road";
    class[1].adr.city = "mumbai";

    printf( " Marks are %d\n", class[1].marks);
    printf( " name are %s\n", class[1].name);
    printf( " adr.plot is %s\n", class[1].adr.plot);
    printf( " adr.street is %s\n", class[1].adr.stret);
    printf( " adr.city is %s\n", class[1].adr.city);         
}
  1. Statement A declares the address of a structure containing the members plot, street and city.
  2. Statement B declares a structure having 3 members: name, marks, and adr. The data type of adr is structure address, which is given by statement C.
  3. Statement D defines the variable student1 of the data type struct student.
  4. Statement E defines an array class with 20 elements. Each element is a structure.
  5. You can refer to marks of the students of class[1] using the notation class[1].marks. class[1] indicates the first element of the array, and since each element is a structure, a member can be accessed using dot notation.
  6. You can refer to the plot of a student of class[1] using the notation class[1].adr.plot. Since the third element of the structure is adr, and plot is a member of adr, you can refer to members of the nested structures.
  7. If you want to refer to the first character of the character array plot, then you can refer it as
8.      Class[1].adr.plot[0]

because plot is a character array. 

0 Comment "When a structure is a member of another structure it is called a nested structure."

Post a Comment