Pointer to Union पॉइंटर टू यूनियन or Pointer of Union पॉइंटर ऑफ़ यूनियन or Union Pointer यूनियन पॉइंटर
A pointer variable of union type which stores the memory address of a union variable of the same type is called a pointer to union or union pointer.
एक यूनियन टाइप का पॉइंटर वेरिएबल जो उसी प्रकार के यूनियन वेरिएबल का मेमोरी एड्रेस स्टोर करता है, पॉइंटर टू यूनियन (Pointer to Union) कहलाता है।
To access the data members of a union using a union pointer, we use the arrow operator (->).
यूनियन पॉइंटर की सहायता से यूनियन के डेटा मेंबर्स को एक्सेस करने के लिए एरो ऑपरेटर (->) का प्रयोग किया जाता है।
Syntax
Declaration:
union union_name *ptr_name;Initialization:
ptr_name = &union_variable_name;Accessing union members:
ptr_name->data_member;C Program for Pointer to Union
#include <stdio.h>
#include <conio.h>
union student
{
int roll_no;
char name[20];
char gender;
float fees;
};
void main()
{
union student s, *p;
p = &s;
clrscr();
printf("Enter roll no of a student:\n");
scanf("%d", &p->roll_no);
printf("Roll number of a student = %d\n", p->roll_no);
getch();
}Output
Enter roll no of a student:
101
Roll number of a student = 101Explanation
In the above program:
union student s;declares a union variables.union student *p;declares a union pointerp.p = &s;stores the address of union variablesin pointerp.p->roll_noaccesses theroll_nomember of unions.scanf("%d", &p->roll_no);inputs the roll number through the union pointer.printf("%d", p->roll_no);displays the roll number using the union pointer.
उपरोक्त प्रोग्राम में:
union student s;एक यूनियन वेरिएबलsडिक्लेयर करता है।union student *p;एक यूनियन पॉइंटरpडिक्लेयर करता है।p = &s;यूनियन वेरिएबलsका एड्रेस पॉइंटरpमें स्टोर करता है।p->roll_noयूनियन केroll_noमेंबर को एक्सेस करता है।scanf("%d", &p->roll_no);यूनियन पॉइंटर की सहायता से रोल नंबर इनपुट करता है।printf("%d", p->roll_no);यूनियन पॉइंटर की सहायता से रोल नंबर डिस्प्ले करता है।
Note:
In a union, all data members share the same memory location. At a time, the union is intended to hold the value of one member.
यूनियन में सभी डेटा मेंबर्स एक ही मेमोरी लोकेशन शेयर करते हैं। एक समय में यूनियन में सामान्यतः किसी एक मेंबर की वैल्यू स्टोर की जाती है।
Comments
Post a Comment