Pointer to Structure पॉइंटर टू स्ट्रक्चर or pointer of structure पॉइंटर ऑफ़ स्ट्रक्चर or structure pointer स्ट्रक्चर पॉइंटर

 A pointer variable of structure type which stores the memory address of a structure variable of the same type is called a pointer to structure or structure pointer.

एक स्ट्रक्चर टाइप का पॉइंटर वेरिएबल जो उसी प्रकार के स्ट्रक्चर वेरिएबल का मेमोरी एड्रेस स्टोर करता है, पॉइंटर टू स्ट्रक्चर (Pointer to Structure) कहलाता है।

To access the data members of a structure using a structure pointer, we use the arrow operator (->).

स्ट्रक्चर पॉइंटर की सहायता से स्ट्रक्चर के डेटा मेंबर्स को एक्सेस करने के लिए एरो ऑपरेटर (->) का प्रयोग किया जाता है।

Syntax

Declaration:

struct structure_name *ptr_name;

Initialization:

ptr_name = &struct_variable_name;

Accessing structure members:

ptr_name->data_member;

C Program for Pointer to Structure

#include <stdio.h>

struct student
{
    int roll_no;
    char name[20];
    char gender;
    float fees;
};

int main()
{
    struct student s, *p;

    p = &s;

    printf("Enter details of student\n");

    printf("Enter roll no:\n");
    scanf("%d", &p->roll_no);

    printf("Enter name:\n");
    scanf("%19s", p->name);

    printf("Enter gender:\n");
    scanf(" %c", &p->gender);

    printf("Enter fees:\n");
    scanf("%f", &p->fees);

    printf("\nDetails of a student\n");

    printf("Roll no = %d\n", p->roll_no);
    printf("Name = %s\n", p->name);
    printf("Gender = %c\n", p->gender);
    printf("Fees = %.2f\n", p->fees);

    return 0;
}

Output

Enter details of student
Enter roll no:
101
Enter name:
Rahul
Enter gender:
M
Enter fees:
2500

Details of a student
Roll no = 101
Name = Rahul
Gender = M
Fees = 2500.00

Explanation

In the above program:

  • struct student s; declares a structure variable s.

  • struct student *p; declares a structure pointer p.

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

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

  • p->name accesses the name member of structure s.

  • p->gender accesses the gender member of structure s.

  • p->fees accesses the fees member of structure s.

उपरोक्त प्रोग्राम में:

  • struct student s; एक स्ट्रक्चर वेरिएबल s डिक्लेयर करता है।

  • struct student *p; एक स्ट्रक्चर पॉइंटर p डिक्लेयर करता है।

  • p = &s; स्ट्रक्चर वेरिएबल s का एड्रेस पॉइंटर p में स्टोर करता है।

  • p->roll_no स्ट्रक्चर के roll_no मेंबर को एक्सेस करता है।

  • p->name स्ट्रक्चर के name मेंबर को एक्सेस करता है।

  • p->gender स्ट्रक्चर के gender मेंबर को एक्सेस करता है।

  • p->fees स्ट्रक्चर के fees मेंबर को एक्सेस करता है।

Note

The arrow operator (->) is used to access structure members through a structure pointer.

The following two expressions are equivalent:

p->roll_no
(*p).roll_no

Both expressions access the roll_no member of the structure variable pointed to by p.

एरो ऑपरेटर (->) का प्रयोग स्ट्रक्चर पॉइंटर के द्वारा स्ट्रक्चर मेंबर्स को एक्सेस करने के लिए किया जाता है।

उपरोक्त दोनों एक्सप्रेशन समान हैं तथा स्ट्रक्चर पॉइंटर p के द्वारा roll_no मेंबर को एक्सेस करते हैं।

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