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 = 101

Explanation

In the above program:

  • union student s; declares a union variable s.

  • union student *p; declares a union pointer p.

  • p = &s; stores the address of union variable s in pointer p.

  • p->roll_no accesses the roll_no member of union s.

  • 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

Popular posts from this blog

C Language Topics in Hindi and English

Top High-Paying Tech Skills to Learn in 2025

Complete C Programming Syllabus | Advanced C Roadmap for Students