Pointer To Union
- As Pointer are the variable which stores the address of other variable.
- So , Now we will learn how store the address of Union variable.
- As you know pointer can point to a variable, only if their data type are same.
- For example:int type of pointer can only point to int type of variable or array,etc
- Because of that ,for pointing a Union you will need to make a pointer of Union datatype. So now we will learn how to do all that things
- So , let start doing it.
- for learning that we will define a Union student as name , rollno ,div as it's member.
Definition:
As we have define a union, now we will declare a pointer of union student data type.
Syntax:declaring pointer of union student data type
union union_name *pointer_variable_name;
union union_name *pointer_variable_name;
Declaring:pointer of union student datatype.
union student *s;
union student *s;
Let's see memory view of union pointer,by looking it you will get an great understanding of pointer of union variable
Now we will access the member of pointer of union variable
Syntax: Accessing the member
pointer_variable_name -> member_name;
- We use arrow operator for accessing the member of union, when union is point by a pointer.
- -> Arrow operator.
pointer_variable_name -> member_name;
Accessing the members:
//accessing rollno
s -> rollno;
//Accessing Name
s -> name;
//Accessing div
s -> div
//accessing rollno s -> rollno; //Accessing Name s -> name; //Accessing div s -> div
Now we will take members as input from the user.
void main()
{
int i;
//declaration of pointer variable of
//union student data type.
union student *s,s1;
s = &s1;
//Assigning address of union
//variable s1 to pointer variable 5
//Taking student roll no as input
printf("\nEnter the roll no of student:");
scanf("%d",&s->rollno);
//Taking student name as input
printf("\nEnter the name of student:");
scanf("%s",s->name);
//Taking student Division as input
printf("\nEnter the student div:");
(s->div) = getche();
//Printing the values which is taking from the user.
printf("student roll no and name is %d and %s and studies in %c Division",s->rollno,s->name,s->div);
}
void main() { int i; //declaration of pointer variable of //union student data type. union student *s,s1; s = &s1; //Assigning address of union //variable s1 to pointer variable 5 //Taking student roll no as input printf("\nEnter the roll no of student:"); scanf("%d",&s->rollno); //Taking student name as input printf("\nEnter the name of student:"); scanf("%s",s->name); //Taking student Division as input printf("\nEnter the student div:"); (s->div) = getche(); //Printing the values which is taking from the user. printf("student roll no and name is %d and %s and studies in %c Division",s->rollno,s->name,s->div); }
Output:
Enter the roll no of student: 1
Enter the name of student: Roy
Enter the student div:A
student roll no and name is 1 and Roy and studies in A Division.
Enter the name of student: Roy
Enter the student div:A
student roll no and name is 1 and Roy and studies in A Division.
Dynamic memory allocation
- Sometimes, the number of union variables you declared may be insufficient. You may need to allocate memory during run-time. Here's how you can achieve this in C programming
In this program we will use malloc() function
Syntax:
Allocating memory dynamically: to pointer of student union datatype
void main()
{
//declaration of pointer variable of
//union student data type.
union student *s;
s = (union student*)malloc(sizeof(union student)*1);
//allocating memory dynamically using malloc()
void main() { //declaration of pointer variable of //union student data type. union student *s; s = (union student*)malloc(sizeof(union student)*1); //allocating memory dynamically using malloc()
Let's take input from the user
Let's print the data on the screen
//printing rollno
printf("\n%d",s->rollno);
//printing name
printf("\n%s\N",s->name);
//printing div
printf("\n%c",s->div);
}
//printing rollno printf("\n%d",s->rollno); //printing name printf("\n%s\N",s->name); //printing div printf("\n%c",s->div); }
- This how we can point to a union variable dynamically.
- So ,do more practice on pointers and union 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