Notes in Hindi

Accessing Structure Members in Hindi

/ BCA / Programming with C and CPP

Accessing Structure Members in Hindi

Accessing Structure Members in Hindi

What is Structure in C/C++?

Structure एक user-defined data type होता है जो हमें एक से अधिक अलग-अलग type के variables को एक साथ store करने की सुविधा देता है। जब हम Structure बनाते हैं, तो हम एक नया data type बना लेते हैं जिसके अंदर multiple members होते हैं। हर member अलग-अलग data type का हो सकता है। Structure का उपयोग real-life entities जैसे – student, employee, book आदि की पूरी जानकारी एक ही जगह store करने के लिए किया जाता है।

Accessing Members of Structure

जब हम Structure बनाते हैं और उसमें कुछ members define करते हैं, तो हमें उन members को access भी करना होता है, यानी उनकी value को read या write करना होता है। Structure members को access करने के लिए दो main तरीके होते हैं:

  • Dot (.) Operator का उपयोग करके
  • Pointer और Arrow (->) Operator का उपयोग करके

Accessing Members Using Dot Operator in Hindi

जब हम Structure का कोई object बनाते हैं और उसमें से किसी member को access करना चाहते हैं, तो हम dot (.) operator का उपयोग करते हैं। यह operator object और उसके member के बीच में लगाया जाता है।

Syntax:

structureVariable.memberName;

Example:

#include <stdio.h> struct Student { int roll; char name[50]; float marks; }; int main() { struct Student s1; s1.roll = 101; strcpy(s1.name, "Rahul"); s1.marks = 89.5; printf("Roll No: %d\n", s1.roll); printf("Name: %s\n", s1.name); printf("Marks: %.2f\n", s1.marks); return 0; }

ऊपर दिए गए program में हमने एक Structure बनाया जिसका नाम `Student` है और उसमें तीन members हैं – roll (int), name (char array), और marks (float)। फिर `s1` नाम का Structure variable बनाया और उसमें सभी values assign कीं। सभी values को access करने के लिए dot operator का उपयोग किया गया।

Member Initialization and Access in Hindi

Structure के members को दो तरीकों से initialize किया जा सकता है:

  • Direct Assignment के द्वारा
  • Initialization at Declaration के द्वारा

1. Direct Assignment

जैसे ही structure variable बनता है, उसके बाद हम dot operator का उपयोग करके हर member को manually assign कर सकते हैं।

struct Employee { int id; char name[30]; float salary; }; int main() { struct Employee e1; e1.id = 1; strcpy(e1.name, "Amit"); e1.salary = 50000; printf("ID: %d, Name: %s, Salary: %.2f", e1.id, e1.name, e1.salary); return 0; }

2. Initialization at Declaration

जब हम structure variable को declare करते हैं उसी समय हम सभी values initialize कर सकते हैं। यह तरीका short और clean होता है।

struct Employee { int id; char name[30]; float salary; }; int main() { struct Employee e2 = {2, "Rohit", 60000}; printf("ID: %d, Name: %s, Salary: %.2f", e2.id, e2.name, e2.salary); return 0; }

ध्यान देने योग्य बात यह है कि इस प्रकार के initialization में members की order वैसी ही होनी चाहिए जैसी structure में define की गई है।

Structure Access with Pointers in Hindi

जब हम Structure को pointer के द्वारा access करते हैं, तब dot operator काम नहीं करता। उसके बदले में हम arrow operator (`->`) का उपयोग करते हैं। यह operator Structure pointer के द्वारा किसी member को access करने के लिए उपयोग किया जाता है।

Syntax:

structurePointer->memberName;

Example:

#include <stdio.h> #include <string.h> struct Book { int id; char title[50]; float price; }; int main() { struct Book b1 = {101, "C Programming", 299.99}; struct Book *ptr; ptr = &b1; printf("Book ID: %d\n", ptr->id); printf("Title: %s\n", ptr->title); printf("Price: %.2f\n", ptr->price); return 0; }

ऊपर दिए गए program में `ptr` एक pointer है जो structure `Book` के variable `b1` को point कर रहा है। अब हम `ptr->id`, `ptr->title`, और `ptr->price` का उपयोग करके members को access कर सकते हैं।

Pointer के उपयोग के फायदे

  • Memory efficient code बनता है
  • Function में Structure को reference के द्वारा भेज सकते हैं जिससे copy की जरूरत नहीं होती
  • Large data structures में performance अच्छा रहता है

Structure Pointer को Function में भेजना

हम किसी Structure को function में pointer के रूप में pass कर सकते हैं जिससे original value पर काम किया जा सके।

#include <stdio.h> struct Student { int roll; float marks; }; void display(struct Student *s) { printf("Roll: %d, Marks: %.2f", s->roll, s->marks); } int main() { struct Student s1 = {10, 87.5}; display(&s1); return 0; }

ऊपर के उदाहरण में Structure को function में pointer के रूप में भेजा गया है और arrow operator का उपयोग करके values access की गई हैं। इससे original data modify भी किया जा सकता है।

Structure और Array

अगर हमें एक ही तरह के कई Structure को store करना हो, तो हम Structure का Array भी बना सकते हैं। इसे हम एक real-world database की तरह use कर सकते हैं।

#include <stdio.h> #include <string.h> struct Student { int id; char name[20]; }; int main() { struct Student s[3]; for (int i = 0; i < 3; i++) { s[i].id = i + 1; sprintf(s[i].name, "Student%d", i + 1); } for (int i = 0; i < 3; i++) { printf("ID: %d, Name: %s\n", s[i].id, s[i].name); } return 0; }

इस प्रकार हम Structure के माध्यम से कई students, employees, books आदि की information manage कर सकते हैं और उसे access करने के लिए dot या arrow operator का use कर सकते हैं।

Structure के अंदर Structure

Structure के अंदर हम किसी दूसरे Structure को भी define कर सकते हैं, इसे Nesting of Structures कहा जाता है।

#include <stdio.h> struct Date { int day; int month; int year; }; struct Student { int roll; struct Date dob; }; int main() { struct Student s1; s1.roll = 1; s1.dob.day = 15; s1.dob.month = 8; s1.dob.year = 2000; printf("Roll: %d, DOB: %d-%d-%d", s1.roll, s1.dob.day, s1.dob.month, s1.dob.year); return 0; }

इस program में हमने `Date` नाम का Structure बनाया और उसे `Student` Structure के अंदर एक member के रूप में use किया है। फिर dot operator के द्वारा nested members को access किया गया है।

FAQs

Structure में dot operator (.) का उपयोग structure variable के किसी member को access करने के लिए किया जाता है। यह operator structure variable और उसके member के बीच लगाया जाता है, जैसे – s1.name
Pointer से structure members को access करने के लिए arrow operator (->) का उपयोग किया जाता है। जब कोई structure pointer किसी variable को point करता है, तब हम उस pointer से member को ऐसे access करते हैं: ptr->name
Structure members को दो तरीकों से initialize किया जा सकता है: 1) Direct assignment जैसे s1.id = 101; और 2) Declaration के समय जैसे struct Student s1 = {101, "Amit", 89.5};
हाँ, हम structure pointer को function में भेज सकते हैं। इससे structure की original values को directly access और modify किया जा सकता है। Function में arrow operator से member access होता है।
जब किसी structure के अंदर दूसरा structure होता है तो उसे nested structure कहते हैं। इसका उपयोग complex data को organize करने के लिए किया जाता है, जैसे – student की DOB को अलग structure में रखना।

Please Give Us Feedback