Array of Unions
- Before taking a deep dive into Array of Unions, We suggest you please go through this topic : What are Union? and what are Array?
- Till now we have seen array of basic data types like array of int ,array of float ,array character etc.
- But now we will see how to make array of Unions variable.
- So,let's define a Union
Let's define a Union Student with name ,rollno,div as it's attribute.
Definition:
Syntax:Declaration of array of union
union union_name union_variable_name[size_of_array];
union union_name union_variable_name[size_of_array];
Declaration of array of union student
void main()
{
//declaration of student array.
union student s[10];
}
void main() { //declaration of student array. union student s[10]; }
How to Access members of union array.
- As,you know every array element has an unique index number,Which starts from 0 index.
- Means first array element wil have index 0,second array element will have index 1 and So on...
Syntax | Accessing member of union array
union_variable_name[index_number].member_name;
union_variable_name[index_number].member_name;
Let's see syntax of accessing the members of student array.
Syntax of Accessing: Rollno of array student
array_name[index_number].rollno;
array_name[index_number].rollno;
Syntax of Accessing: Name of array student
array_name[index_number].name;
array_name[index_number].name;
Syntax of Accessing: Div of array student
array_name[index_number].div;
array_name[index_number].div;
- This is how we access the member of union array.
Now , we will see program of taking members of union as Input.
Taking Input:
void main()
{
int i;
//declaration of student array.
union student s[3];
for(i=0; i<3 ;i++)
{
printf("\nEnter name of student: ");
gets(s[i].name);//taking name of student
printf("\nEnter the student roll no:");
scanf("%d",&s[i].rollno);//taking student rollno
printf("\nstudent division:");
scanf("%c",s[i].div);//taking student division
}
}
void main() { int i; //declaration of student array. union student s[3]; for(i=0; i<3 ;i++) { printf("\nEnter name of student: "); gets(s[i].name);//taking name of student printf("\nEnter the student roll no:"); scanf("%d",&s[i].rollno);//taking student rollno printf("\nstudent division:"); scanf("%c",s[i].div);//taking student division } }
Output:
Enter name of student: Roy
Enter the student roll no: 1
student division: A
Enter name of student: Jason
Enter the student roll no: 2
student division: B
Enter name of student: Tom
Enter the student roll no: 3
student division: A
Enter the student roll no: 1
student division: A
Enter name of student: Jason
Enter the student roll no: 2
student division: B
Enter name of student: Tom
Enter the student roll no: 3
student division: A
Printing the student data:
int i;
for(i=0; i<3 ;i++)
{
printf("\nstudent roll no and Name is %d and %s and studies in %c division",s[i].rollno,s[i].name,s[i].div);
}
int i; for(i=0; i<3 ;i++) { printf("\nstudent roll no and Name is %d and %s and studies in %c division",s[i].rollno,s[i].name,s[i].div); }
Output:
student roll no and Name is 1 and Roy and studies in A division
student roll no and Name is 2 and Jason and studies in B division
student roll no and Name is 3 and Tom and studies in A division
student roll no and Name is 2 and Jason and studies in B division
student roll no and Name is 3 and Tom and studies in A division
- This how we can take members of union as input from User.
- This how we can work with array of union In C.
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