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];
};
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.
};
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");
#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;
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);
}
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;
s1.n1.first_name;
Accessing: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.
};
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:
- How to initialize an structure variable?
- How to Access Structure member?
- What are Nested Structure?
- How to built an Nested Structure?
- How to Access members of Nested Structure?
- How to initialize Nested structure?
- Functions and Structure
- How to pass a structure variable to function?
- How to make function which will take structure variable as arguments?
- How to make function which will return structure variable?
- Array and Structure
- How to make array of structure variable?
- How to initialize array structure variable?
- How to Access members of an array of structure?
- How to take members of array structure as input from the user?
- Pointer and Structure
- How to Point a structure variable?
- How to declare a pointer of structure variable?
- How to Initialize an pointer of structure variable?
- How to access member of structure variable which is point by an pointer?
- What is '->' Operator?
- How to allocate memory for structure variable Dynamically
- How to allocate memory of structure variable using malloc() and calloc() function
- Self Referential Block
- What is Referential Structure?
- How to Create self Referential Structure?
- Applications of Self Referential Structures
- Practice Programs
People Also Searched:
- What are structures?
- Why structures?
- Need of Structure
- Concept of structure
- Array and Pointer
- What is array of pointer?
- how to initialize an array of pointers?
- How to Access elements of an Array Of Pointers?
- Functions and Pointers
- How to pass a Pointer To a Function?
- Call By reference
- Functions In C
- Why Functions?
- What are Functions?
- how to write our functions?
- What are programmer define functions?
- How to declare function?
- How to define a function?
- What is function Prototype?
- how to declare a function?
- what are Actual Parameters?
- What are Formal Parameters?
- Function Call?
- how to call a Function?
- What are different types of calling function?
- Call by value
- call by reference
- Recursion
- Array and Functions
- How to pass an array to a function?
- What is Character array
- Functions for Characters
- Functions from #include<ctype.h> Header File
- what are isdigit(), islower() , isupper(),etc
- what are various string handling functions
- Functions from #include<string.h> Header File
- what are strcpy(), strcmp(), strncmp(), strcmpi(),etc
- strcmp() VS strncmp() VS strcmpi()
- Input And Output Functions
- How to take strings As Input
- What is gets() functions
- What is puts() function
0 Comments
Post a Comment