Nested Structure
- Nested structure is structure which has another structure as a member in that structure.
- A member of structure can be structure itself , this what we call as Nested Structure.
- Let's understand it by an example.
let's define a structure student with roll no , div and name but should be structure which will have first name , surname as it's attribute.
Defining: structure name
struct name
{
char first_name[20];
char surname_name[20];
};
struct name { char first_name[20]; char surname_name[20]; };
Defining:Structure Student and using struture name as member in structure Student
struct student //structure name
{
int rollno; //member1 ,integer variable which will store,
//student roll no.
char div; //member2 a character variable,
//which will store division of student.
struct name n1; //member3 a structure name
//which contains first name and
//last name as its member.
};
struct student //structure name { int rollno; //member1 ,integer variable which will store, //student roll no. char div; //member2 a character variable, //which will store division of student. struct name n1; //member3 a structure name //which contains first name and //last name as its member. };
Let's understand visually
Now let's initialize the nested structure.
Initializing:
struct student s1 = {1,A,{"Joel","Anthony"}};
struct student s1 = {1,A,{"Joel","Anthony"}};
As member3 n1 is a structure name.
that is why we have used another curly braces for member3
Now let's access the member of student structure
Accessing:member rollno and div
main()
{
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;
}
main() { 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 structure name.
this is something new which we will going learn now.
Accessing:Member n1
Let's access member n1 individually
Accessing:first_name
s1.n1.first_name;
s1.n1.first_name;
Accessing:last_name
s1.n1.last_name;
s1.n1.last_name;
- This is how we can make a nested structure.
- There is a another way of defining nested structure but method accessing and initialization of members will remain same
- Let's see the another method of defining nested structure
- We will define the same structure student by another method
Defining:structure student by another method
struct student
{
int rollno; //member1 ,integer variable which will store,
//student roll no.
char div; //member2 a character variable,
//which will store division of student.
struct name
{
char first_name[20]; //member3 a structure name
//which contains first name and
//last name as its member.
char surname_name[20];
}n1;//declaring variable n1
//of structure name data type.
};
struct student { int rollno; //member1 ,integer variable which will store, //student roll no. char div; //member2 a character variable, //which will store division of student. struct name { char first_name[20]; //member3 a structure name //which contains first name and //last name as its member. char surname_name[20]; }n1;//declaring variable n1 //of structure name data type. };
- This is how we can build a nested structure.
- Do practice on structures for understanding the concept more better.
Further Concept:
- 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
- How to initialize an structure variable?
- How to Access Structure member?
- 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