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.00Explanation
In the above program:
struct student s;declares a structure variables.struct student *p;declares a structure pointerp.p = &s;stores the address of structure variablesin pointerp.p->roll_noaccesses theroll_nomember of structures.p->nameaccesses thenamemember of structures.p->genderaccesses thegendermember of structures.p->feesaccesses thefeesmember of structures.
उपरोक्त प्रोग्राम में:
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_noBoth expressions access the roll_no member of the structure variable pointed to by p.
एरो ऑपरेटर (->) का प्रयोग स्ट्रक्चर पॉइंटर के द्वारा स्ट्रक्चर मेंबर्स को एक्सेस करने के लिए किया जाता है।
उपरोक्त दोनों एक्सप्रेशन समान हैं तथा स्ट्रक्चर पॉइंटर p के द्वारा roll_no मेंबर को एक्सेस करते हैं।
Comments
Post a Comment