Pointers, Arrays and Structures
Combining pointers with arrays and structures, and understanding reference variables.
Pointers, Arrays and Structures
C++ में Pointers, Arrays और Structures तीन महत्वपूर्ण concepts हैं। इनका एक-दूसरे के साथ close relationship होता है। Pointer का उपयोग array के elements को access करने, structure के members तक पहुँचने और functions के साथ data को efficiently handle करने के लिए किया जा सकता है।
Pointer and Array
Array में elements लगातार (contiguous) memory locations में store होते हैं। Array का नाम कई expressions में उसके first element के address के रूप में काम करता है। इसलिए pointer की सहायता से array के elements को access किया जा सकता है।
यदि arr एक array है, तो सामान्यतः:
अर्थात arr array के first element का address represent करता है।
Accessing Array Elements Using Pointer
Pointer arithmetic की सहायता से array के अलग-अलग elements को access किया जा सकता है।
#include <iostream>
using namespace std;
int main()
{
int arr[5] = {10, 20, 30, 40, 50};
int *p = arr;
cout << *p << endl;
cout << *(p + 1) << endl;
cout << *(p + 2) << endl;
cout << *(p + 3) << endl;
cout << *(p + 4);
return 0;
}
10
20
30
40
50
यहाँ p array के first element को point करता है। *(p + 1) दूसरे element, *(p + 2) तीसरे element और इसी प्रकार आगे के elements को access करता है।
Pointer Arithmetic with Arrays
Array के साथ pointer arithmetic करते समय pointer को increment या decrement किया जा सकता है। Pointer का एक element आगे बढ़ना उस data type के size के अनुसार होता है।
#include <iostream>
using namespace std;
int main()
{
int arr[4] = {5, 10, 15, 20};
int *p = arr;
for (int i = 0; i < 4; i++)
{
cout << *p << " ";
p++;
}
return 0;
}
5 10 15 20
हर बार p++ करने पर pointer array के अगले element की ओर move करता है।
Array of Pointers
C++ में pointers का array भी बनाया जा सकता है। इसे Array of Pointers कहा जाता है। इसमें array का प्रत्येक element एक pointer होता है।
#include <iostream>
using namespace std;
int main()
{
int a = 10, b = 20, c = 30;
int *p[3] = {&a, &b, &c};
cout << *p[0] << endl;
cout << *p[1] << endl;
cout << *p[2];
return 0;
}
10
20
30
यहाँ p एक ऐसा array है जिसके तीनों elements integer pointers हैं।
Pointer to an Array
Pointer to an Array ऐसा pointer है जो पूरे array को point करता है। इसका declaration सामान्य array pointer से अलग होता है।
int (*p)[5];
यहाँ p पाँच integers वाले एक पूरे array को point कर सकता है।
Pointers and Structures
Structure एक user-defined data type है जिसमें अलग-अलग data types के related members को एक single unit में group किया जाता है। Structure के object का address pointer में store किया जा सकता है।
#include <iostream>
using namespace std;
struct Student
{
int roll;
float marks;
};
int main()
{
Student s = {101, 88.5};
Student *p = &s;
cout << "Roll: " << p->roll << endl;
cout << "Marks: " << p->marks;
return 0;
}
Roll: 101
Marks: 88.5
यहाँ p, structure object s का address store करता है। Structure pointer के members को access करने के लिए -> operator का उपयोग किया गया है।
Structure object → . operator
Structure pointer → -> operator
Dot (.) Operator and Arrow (->) Operator
| Operator | Used With | Example |
|---|---|---|
| . | Structure object | s.roll |
| -> | Pointer to structure | p->roll |
यदि s structure object है और p उसी object का pointer है, तो:
Accessing Structure Members Using Pointer
Structure pointer के members को दो तरीकों से access किया जा सकता है।
#include <iostream>
using namespace std;
struct Student
{
int roll;
int marks;
};
int main()
{
Student s = {101, 95};
Student *p = &s;
cout << (*p).roll << endl;
cout << (*p).marks << endl;
cout << p->roll << endl;
cout << p->marks;
return 0;
}
101
95
101
95
दोनों methods समान result देते हैं। -> operator structure pointer के members को access करने का अधिक convenient तरीका है।
Array of Structures
जब एक ही प्रकार के कई structure records store करने हों, तो Array of Structures का उपयोग किया जा सकता है।
#include <iostream>
using namespace std;
struct Student
{
int roll;
float marks;
};
int main()
{
Student s[3] = {
{101, 85.5},
{102, 90.0},
{103, 78.5}
};
for (int i = 0; i < 3; i++)
{
cout << s[i].roll << " "
<< s[i].marks << endl;
}
return 0;
}
101 85.5
102 90
103 78.5
Pointer with Array of Structures
Array of structures के elements को pointer की सहायता से भी access किया जा सकता है। Structure pointer को array के first element का address दिया जा सकता है।
#include <iostream>
using namespace std;
struct Student
{
int roll;
float marks;
};
int main()
{
Student s[3] = {
{101, 85.5},
{102, 90.0},
{103, 78.5}
};
Student *p = s;
for (int i = 0; i < 3; i++)
{
cout << p->roll << " "
<< p->marks << endl;
p++;
}
return 0;
}
101 85.5
102 90
103 78.5
यहाँ p array के first structure element को point करता है। p++ करने पर pointer अगले structure element पर move करता है।
Passing Array of Structures to a Function
Array of structures को function में pass करते समय उसका address या pointer उपयोग किया जा सकता है। इससे function array के elements को access कर सकता है।
#include <iostream>
using namespace std;
struct Student
{
int roll;
float marks;
};
void display(Student *p, int n)
{
for (int i = 0; i < n; i++)
{
cout << p[i].roll << " "
<< p[i].marks << endl;
}
}
int main()
{
Student s[2] = {
{101, 88.5},
{102, 92.0}
};
display(s, 2);
return 0;
}
101 88.5
102 92
Structure Containing a Pointer
Structure के अंदर pointer member भी declare किया जा सकता है। इससे structure किसी अन्य variable या dynamically allocated data को refer कर सकता है।
#include <iostream>
using namespace std;
struct Data
{
int value;
int *ptr;
};
int main()
{
int x = 50;
Data d;
d.value = 100;
d.ptr = &x;
cout << "Value: " << d.value << endl;
cout << "Pointer Value: " << *d.ptr;
return 0;
}
Value: 100
Pointer Value: 50
यहाँ structure Data में ptr एक pointer member है, जो variable x का address store करता है।
Pointer to Structure and Dynamic Memory
Structure objects के लिए dynamic memory allocation भी की जा सकती है। इसके लिए new operator का उपयोग किया जा सकता है।
#include <iostream>
using namespace std;
struct Student
{
int roll;
float marks;
};
int main()
{
Student *p = new Student;
p->roll = 101;
p->marks = 89.5;
cout << "Roll: " << p->roll << endl;
cout << "Marks: " << p->marks;
delete p;
return 0;
}
Roll: 101
Marks: 89.5
यहाँ new operator से Student object के लिए memory allocate की गई है और p pointer उस object को point करता है। उपयोग पूरा होने के बाद delete operator से allocated memory release की गई है।
Pointer, Array and Structure Together
एक program में pointer, array और structure तीनों का उपयोग करके records को efficiently handle किया जा सकता है।
#include <iostream>
using namespace std;
struct Student
{
int roll;
int marks;
};
int main()
{
Student students[3] = {
{1, 80},
{2, 90},
{3, 85}
};
Student *p = students;
for (int i = 0; i < 3; i++)
{
cout << "Roll: " << p->roll
<< ", Marks: " << p->marks
<< endl;
p++;
}
return 0;
}
Roll: 1, Marks: 80
Roll: 2, Marks: 90
Roll: 3, Marks: 85
Important Points
- Array का नाम कई expressions में first element का address represent करता है।
- Pointer arithmetic का उपयोग array elements को access करने के लिए किया जा सकता है।
- Pointer का उपयोग structure object के address को store करने के लिए किया जा सकता है।
- Structure pointer के members को access करने के लिए -> operator का उपयोग किया जाता है।
- Structure object के members को access करने के लिए . operator का उपयोग किया जाता है।
- p->member और (*p).member समान अर्थ रखते हैं।
- Array of structures में एक ही प्रकार के कई records store किए जा सकते हैं।
- Array of structures को pointer की सहायता से sequentially access किया जा सकता है।
- Structure के अंदर pointer member declare किया जा सकता है।
- Dynamic structure object बनाने के लिए new और memory release करने के लिए delete का उपयोग किया जा सकता है।
Board Focus
arr → First element का address
*p → Pointer द्वारा pointed value
p + i → Array के i-th element की ओर pointer
s.member → Structure object का member
p->member → Structure pointer का member
p->member = (*p).member
Student s[10] → Array of Structures
Student *p = s → Structure array के first element को point करना
Board Important Questions
Very Short Answer Questions
Q1. Array और pointer के बीच क्या संबंध है?
Answer: Array का नाम कई expressions में उसके first element के address को represent करता है, इसलिए pointer की सहायता से array elements को access किया जा सकता है।
Q2. Structure pointer क्या है?
Answer: ऐसा pointer जो किसी structure object का address store करता है, structure pointer कहलाता है।
Q3. Structure pointer के members को access करने के लिए कौन-सा operator उपयोग होता है?
Answer: -> operator का उपयोग होता है।
Q4. Structure object के member को access करने के लिए कौन-सा operator उपयोग होता है?
Answer: . operator का उपयोग होता है।
Q5. p->roll किसके बराबर है?
Answer: p->roll, (*p).roll के बराबर है।
Q6. Array of Structures क्या है?
Answer: एक ही structure type के कई objects का collection Array of Structures कहलाता है।
Short Answer Questions
Q7. Pointer की सहायता से array elements को कैसे access किया जाता है?
Answer: Array के first element का address pointer में store करके pointer arithmetic की सहायता से elements को access किया जा सकता है। यदि p array के first element को point करता है, तो *p पहला element और *(p + 1) दूसरा element access करता है।
Q8. . और -> operators में अंतर बताइए।
| . Operator | -> Operator |
|---|---|
| Structure object के साथ उपयोग होता है। | Structure pointer के साथ उपयोग होता है। |
| Example: s.roll | Example: p->roll |
| Direct object member access करता है। | Pointer के माध्यम से member access करता है। |
Q9. Array of Structures क्या है? उदाहरण दीजिए।
Answer: जब एक ही structure type के कई objects को array के रूप में store किया जाता है, तो उसे Array of Structures कहते हैं।
struct Student
{
int roll;
float marks;
};
Student s[3];
यहाँ s तीन Student records को store करने वाला array है।
Q10. Structure pointer के साथ -> operator का उपयोग क्यों किया जाता है?
Answer: जब किसी structure object का address pointer में stored होता है, तब उस object के members को pointer के माध्यम से access करने के लिए -> operator का उपयोग किया जाता है।
Q11. p->member और (*p).member में क्या संबंध है?
Answer: दोनों expressions structure pointer के member को access करते हैं और दोनों का अर्थ समान होता है। p->member वास्तव में (*p).member का convenient form है।
Long Answer Questions
Q12. Pointer की सहायता से array के elements display करने का C++ program लिखिए।
#include <iostream>
using namespace std;
int main()
{
int arr[5] = {10, 20, 30, 40, 50};
int *p = arr;
for (int i = 0; i < 5; i++)
{
cout << *(p + i) << " ";
}
return 0;
}
10 20 30 40 50
Q13. Structure pointer का उपयोग करके structure members display करने का program लिखिए।
#include <iostream>
using namespace std;
struct Student
{
int roll;
float marks;
};
int main()
{
Student s = {101, 95.5};
Student *p = &s;
cout << "Roll: " << p->roll << endl;
cout << "Marks: " << p->marks;
return 0;
}
Roll: 101
Marks: 95.5
Q14. Pointer की सहायता से Array of Structures के records display करने का program लिखिए।
#include <iostream>
using namespace std;
struct Student
{
int roll;
float marks;
};
int main()
{
Student s[3] = {
{101, 80.5},
{102, 91.0},
{103, 87.5}
};
Student *p = s;
for (int i = 0; i < 3; i++)
{
cout << p->roll << " "
<< p->marks << endl;
p++;
}
return 0;
}
101 80.5
102 91
103 87.5
Q15. Array of Structures को function में pass करने का program लिखिए।
#include <iostream>
using namespace std;
struct Student
{
int roll;
int marks;
};
void display(Student *p, int n)
{
for (int i = 0; i < n; i++)
{
cout << p[i].roll << " "
<< p[i].marks << endl;
}
}
int main()
{
Student s[3] = {
{1, 85},
{2, 90},
{3, 95}
};
display(s, 3);
return 0;
}
1 85
2 90
3 95
Quick Revision
- Array: Same data type के elements का collection
- Pointer: Address store करने वाला variable
- arr: कई expressions में first element का address
- *(p + i): Pointer से array element access करना
- Structure Pointer: Structure object का address store करने वाला pointer
- . → Structure object के member के लिए
- -> → Structure pointer के member के लिए
- Array of Structures: Multiple structure records का array
- new: Dynamic memory allocation
- delete: Dynamically allocated memory release करना
Practice Questions
- Array और pointer के बीच संबंध समझाइए।
- Pointer arithmetic क्या है?
- Pointer की सहायता से array elements को access करने का program लिखिए।
- Array of Pointers क्या है?
- Pointer to an Array क्या है?
- Structure pointer क्या है?
- . और -> operators में अंतर बताइए।
- p->member और (*p).member में क्या संबंध है?
- Array of Structures क्या है?
- Pointer की सहायता से Array of Structures के elements access करने का program लिखिए।
- Structure के अंदर pointer member कैसे declare किया जाता है?
- Array of Structures को function में pass करने का program लिखिए।
- Structure pointer का उपयोग करके structure members display करने का program लिखिए।
- Pointer, Array और Structure तीनों का एक साथ उपयोग करने वाला program लिखिए।
- new और delete operators का structure pointers के साथ उपयोग समझाइए।