Nested Union

  • Before taking a deep dive into nested Union, We suggest you please go through this topic : What are Union?
  • Nested Union is Union which has another Union as a member in that Union.
  • A member of Union can be Union itself , this what we call as Nested Union.
  • Let's understand it by an example.

let's define a Union student with roll no , div and name but should be Union which will have first name , surname as it's attribute.

Defining: Union name
union name
{
  char first_name[20];

  char last_name[20];


};

Defining:union Student and using union name as member in union Student
union student  //union name
{
    int  rollno; //member1 ,integer variable which will store,
                 //student roll no.

   

    char  div;  //member2 a character variable,
              //which will store division of student.
    
    union name n1; //member3 a union name
                    //which contains first name and
                    //last name as its member.
};

Let's understand visually





As you know unions can't get initialize so we will assign some value to every member and also we will learn how access the members of Union.

Assigning Some value to each member:
#include<stdio.h>
#include<string.h>
void main()
{
    union student1 s1;

    s1.rollno = 10;
    s1.div = 'A';

    strcpy(s1.n1.first_name, "Joel");
    strcpy(s1.n1.last_name, "Anthony");


As member3 n1 is a union name.

Now let's access the member of student Union

Accessing: member rollno and div and assigning their value to some variable
int s_roll;
char s_div;

//Accessing rollno and assigning
//it to a variable s_roll.
s_roll  = s1.rollno;

//Accessing div and assigning
//it to a variable s_div.
s_div   = s1.div;



Now we will Access the member3 n1 which is the union itself name.
this is something new which we will going learn now.

Accessing: Member n1 and assigning their value to some variable
char f_name[20]
char l_name[20];

//Accessing first name and assigning
//it to a variable f_name.

strcpy( f_name, s1.n1.first_name);

//Accessing div and assigning
//it to a variable l_name
   strcpy(l_name, s1.n1.last_name);


}

See, how we accessed members of n1

Accessing:first_name
s1.n1.first_name;

Accessing:last_name
s1.n1.last_name;

  • This is how we can make a nested structure.
  • There is a another way of defining nested union but method accessing and initialization of members will remain same
  • Let's see the another method of defining nested union
  • We will define the same union student by another method

Defining:union student by another method
union student  
{
    int  rollno; //member1 ,integer variable which will store,
                 //student roll no.


    char  div;  //member2 a character variable,
              //which will store division of student.


    union name
   {
     char first_name[20];   //member3 a structure name
                          //which contains first name and
                           //last name as its member.
     char last_name[20];


    }n1;//declaring variable n1
        //of union name data type.

};

  • This is how we can build a nested union.
  • Do practice on Unions for understanding the concept more better.






Related Concept:



People Also Searched: