Introduction to Pointers
Variables that store the memory address of other variables.
Introduction to Pointers
Pointer C++ का एक special variable (विशेष variable) है जो किसी दूसरे variable का memory address (मेमोरी पता) store करता है। सामान्य variable में किसी value को store किया जाता है, जबकि pointer variable में उस value का address store किया जाता है।
What is a Pointer?
Computer की memory में प्रत्येक variable को एक specific memory location (मेमोरी स्थान) मिलता है। उस memory location का एक address होता है। Pointer उस address को store करने वाला variable है।
उदाहरण के लिए, यदि किसी variable x में 50 store है और उसका memory address 1000 है, तो एक pointer variable p में 1000 store किया जा सकता है।
Address of x: 1000
Pointer p: stores 1000
Why are Pointers Used?
Pointers का उपयोग memory और data को अधिक efficiently handle करने के लिए किया जाता है। C++ में pointers का उपयोग कई महत्वपूर्ण programming tasks में होता है।
- Memory address को store करने के लिए।
- किसी variable को उसके address के माध्यम से access करने के लिए।
- Functions में arguments को reference के रूप में pass करने के लिए।
- Arrays और strings के साथ काम करने के लिए।
- Dynamic memory allocation में।
- Data structures जैसे linked list, tree आदि बनाने में।
Address Operator (&)
C++ में & को address-of operator कहा जाता है। इसका उपयोग किसी variable का memory address प्राप्त करने के लिए किया जाता है।
#include <iostream>
using namespace std;
int main()
{
int x = 50;
cout << "Value: " << x << endl;
cout << "Address: " << &x;
return 0;
}
Value: 50
Address: 0x7ffd2a1c
यहाँ address का actual value प्रत्येक system या program execution में अलग हो सकता है। इसलिए output में दिखाई देने वाला hexadecimal address केवल उदाहरण है।
Declaring a Pointer
Pointer declare करते समय data type के साथ * symbol का उपयोग किया जाता है।
General syntax:
data_type *pointer_name;
उदाहरण:
int *p;
float *q;
char *ch;
यहाँ p एक integer pointer, q एक float pointer और ch एक character pointer है।
Initializing a Pointer
Pointer को किसी variable के address से initialize किया जा सकता है।
#include <iostream>
using namespace std;
int main()
{
int x = 25;
int *p = &x;
cout << "Value of x: " << x << endl;
cout << "Address of x: " << &x << endl;
cout << "Value stored in p: " << p;
return 0;
}
Value of x: 25
Address of x: 0x7ffd2a1c
Value stored in p: 0x7ffd2a1c
यहाँ p में x का address store है। इसलिए p और &x का address समान होगा।
Dereference Operator (*)
Pointer में stored address के माध्यम से उस memory location की value प्राप्त करने के लिए * को dereference operator कहा जाता है।
#include <iostream>
using namespace std;
int main()
{
int x = 50;
int *p = &x;
cout << "Value of x: " << x << endl;
cout << "Value using pointer: " << *p;
return 0;
}
Value of x: 50
Value using pointer: 50
यहाँ *p का अर्थ है उस memory address पर stored value। चूँकि p में x का address है, इसलिए *p से x की value प्राप्त होती है।
&x → Address of x
p → Address stored in pointer
*p → Value stored at that address
Changing Value Using a Pointer
Pointer की सहायता से उस variable की value को भी modify किया जा सकता है जिसका address pointer में stored है।
#include <iostream>
using namespace std;
int main()
{
int x = 10;
int *p = &x;
*p = 20;
cout << "Value of x: " << x;
return 0;
}
Value of x: 20
यहाँ *p के माध्यम से x की value को 10 से बदलकर 20 किया गया है।
Pointer and Variable Relationship
यदि pointer p, variable x का address store करता है, तो निम्न relationship होता है:
*p = x
अर्थात p में x का address होता है और *p से x की value प्राप्त की जा सकती है।
Example of Pointer Relationship
#include <iostream>
using namespace std;
int main()
{
int number = 100;
int *ptr = &number;
cout << "Number: " << number << endl;
cout << "Pointer Address: " << ptr << endl;
cout << "Value through Pointer: " << *ptr;
return 0;
}
Number: 100
Pointer Address: 0x7ffd2a1c
Value through Pointer: 100
Pointer Data Types
Pointer का data type उस data type से related होता है जिसका address वह store करता है।
| Pointer Declaration | Points to |
|---|---|
| int *p; | int variable |
| float *p; | float variable |
| char *p; | char variable |
| double *p; | double variable |
Pointer to Different Data Types
#include <iostream>
using namespace std;
int main()
{
int a = 10;
float b = 5.5;
char c = 'A';
int *p1 = &a;
float *p2 = &b;
char *p3 = &c;
cout << *p1 << endl;
cout << *p2 << endl;
cout << *p3;
return 0;
}
10
5.5
A
Null Pointer
जब pointer किसी valid memory location को point नहीं करता, तो उसे Null Pointer कहा जा सकता है। Modern C++ में pointer को initialize करने के लिए nullptr का उपयोग करना बेहतर तरीका है।
#include <iostream>
using namespace std;
int main()
{
int *p = nullptr;
if (p == nullptr)
{
cout << "Pointer is null";
}
return 0;
}
Pointer is null
Pointer and Array
C++ में arrays और pointers का close relationship होता है। Array का नाम कई expressions में उसके first element के address के रूप में उपयोग किया जा सकता है।
#include <iostream>
using namespace std;
int main()
{
int numbers[3] = {10, 20, 30};
int *p = numbers;
cout << *p << endl;
cout << *(p + 1) << endl;
cout << *(p + 2);
return 0;
}
10
20
30
यहाँ p array के first element को point करता है। p + 1 next element की ओर और p + 2 उसके अगले element की ओर point करता है।
Pointer Arithmetic
Pointer पर कुछ arithmetic operations किए जा सकते हैं। Integer array के साथ pointer को increment करने पर वह अगले element के address पर move करता है।
#include <iostream>
using namespace std;
int main()
{
int arr[3] = {10, 20, 30};
int *p = arr;
cout << *p << endl;
p++;
cout << *p << endl;
p++;
cout << *p;
return 0;
}
10
20
30
Pointer arithmetic data type के size को ध्यान में रखती है। इसलिए p++ integer array में अगले integer element पर move करता है।
Pointers and Functions
Pointer का उपयोग function में variable का address pass करने के लिए किया जा सकता है। इससे function original variable की value को modify कर सकता है।
#include <iostream>
using namespace std;
void changeValue(int *p)
{
*p = 100;
}
int main()
{
int x = 10;
changeValue(&x);
cout << "Value of x: " << x;
return 0;
}
Value of x: 100
यहाँ x का address function को दिया गया है। Function में pointer के माध्यम से original variable की value बदल दी गई है।
Pointer to Pointer
C++ में एक pointer का address store करने वाला pointer भी बनाया जा सकता है। इसे Pointer to Pointer कहा जाता है। इसके declaration में दो * symbols का उपयोग होता है।
#include <iostream>
using namespace std;
int main()
{
int x = 50;
int *p = &x;
int **q = &p;
cout << "Value: " << **q;
return 0;
}
Value: 50
यहाँ p, x का address store करता है और q, p का address store करता है। इसलिए **q से x की value प्राप्त होती है।
Advantages of Pointers
- Memory addresses को directly handle करने की सुविधा मिलती है।
- Functions के माध्यम से original variables को modify किया जा सकता है।
- Arrays और strings के साथ efficient operations किए जा सकते हैं।
- Dynamic memory allocation में pointers आवश्यक होते हैं।
- Linked lists, trees और अन्य dynamic data structures बनाने में उपयोगी हैं।
Precautions While Using Pointers
- Pointer को use करने से पहले properly initialize करना चाहिए।
- Invalid या uninitialized pointer को dereference नहीं करना चाहिए।
- Null pointer को dereference नहीं करना चाहिए।
- Pointer को केवल compatible data type के object का address देना चाहिए।
- Memory management करते समय pointers का सावधानी से उपयोग करना चाहिए।
Important Points
- Pointer एक variable है जो किसी दूसरे variable का memory address store करता है।
- & address-of operator है।
- * dereference operator है।
- p = &x में p, x का address store करता है।
- *p से उस address पर stored value प्राप्त होती है।
- Pointer को data type के अनुसार declare किया जाता है।
- nullptr का उपयोग null pointer को represent करने के लिए किया जा सकता है।
- Arrays और pointers का C++ में close relationship है।
- Pointers को functions में arguments के रूप में pass किया जा सकता है।
- एक pointer दूसरे pointer का address भी store कर सकता है।
Board Focus
&x → Address of x
p → Address stored in pointer
*p → Value at the address
int *p → Integer Pointer
nullptr → Null Pointer
p++ → Pointer को अगले element की ओर move करना
**q → Pointer to Pointer के माध्यम से value access करना
Board Important Questions
Very Short Answer Questions
Q1. Pointer क्या है?
Answer: Pointer एक variable है जो किसी दूसरे variable का memory address store करता है।
Q2. Address-of operator कौन-सा है?
Answer: & address-of operator है।
Q3. Dereference operator कौन-सा है?
Answer: * dereference operator है।
Q4. &x क्या दर्शाता है?
Answer: &x variable x का memory address दर्शाता है।
Q5. *p क्या दर्शाता है?
Answer: *p pointer p द्वारा point किए गए memory location की value दर्शाता है।
Q6. Null pointer क्या है?
Answer: ऐसा pointer जो किसी valid object या memory location को point नहीं करता, null pointer कहलाता है। Modern C++ में nullptr का उपयोग किया जाता है।
Q7. Pointer declaration का सामान्य syntax क्या है?
Answer: data_type *pointer_name; pointer declaration का सामान्य syntax है।
Short Answer Questions
Q8. Pointer और सामान्य variable में अंतर बताइए।
| Variable | Pointer |
|---|---|
| यह सामान्यतः data/value store करता है। | यह किसी variable का memory address store करता है। |
| उदाहरण: int x = 10; | उदाहरण: int *p = &x; |
| x से value directly access की जाती है। | *p से pointed location की value access की जाती है। |
Q9. Address-of और dereference operators को समझाइए।
Answer: Address-of operator & किसी variable का memory address प्राप्त करता है। Dereference operator * pointer में stored address पर मौजूद value को access करता है। उदाहरण के लिए, यदि p = &x है, तो &x address देता है और *p से x की value प्राप्त होती है।
Q10. Pointer का उपयोग functions में क्यों किया जाता है?
Answer: Pointer के माध्यम से function को variable का address दिया जा सकता है। इससे function original variable की value को modify कर सकता है और बड़े data को efficiently handle किया जा सकता है।
Q11. Pointer और array के बीच क्या संबंध है?
Answer: C++ में array का नाम कई expressions में उसके first element के address के रूप में उपयोग किया जा सकता है। Pointer arithmetic की सहायता से array के अन्य elements को भी access किया जा सकता है।
Q12. Pointer to Pointer क्या है?
Answer: ऐसा pointer जो किसी दूसरे pointer का address store करता है, Pointer to Pointer कहलाता है। इसे सामान्यतः ** के साथ declare किया जाता है।
Long Answer Questions
Q13. Pointer को उदाहरण सहित समझाइए।
Answer: Pointer C++ का एक variable है जो किसी दूसरे variable का memory address store करता है। Address-of operator & से variable का address प्राप्त किया जाता है और dereference operator * से pointer द्वारा point की गई value access की जाती है।
#include <iostream>
using namespace std;
int main()
{
int x = 25;
int *p = &x;
cout << "Value: " << *p;
return 0;
}
Value: 25
Q14. Pointer की सहायता से variable की value बदलने का program लिखिए।
#include <iostream>
using namespace std;
int main()
{
int x = 10;
int *p = &x;
*p = 50;
cout << "Value: " << x;
return 0;
}
Value: 50
Q15. Pointer का उपयोग करके array के elements display करने का 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
Q16. Pointer का उपयोग करके function के माध्यम से variable की value बदलने का program लिखिए।
#include <iostream>
using namespace std;
void update(int *p)
{
*p = 500;
}
int main()
{
int number = 100;
update(&number);
cout << "Number: " << number;
return 0;
}
Number: 500
Quick Revision
- Pointer: Memory address store करने वाला variable
- &: Address-of operator
- *: Dereference operator
- p = &x: p में x का address store करना
- *p: x की value access करना
- nullptr: Null pointer
- p++: Pointer को next element पर move करना
- **q: Pointer to Pointer के माध्यम से value access करना
Practice Questions
- Pointer क्या है?
- Pointer declaration का syntax लिखिए।
- Address-of operator क्या है?
- Dereference operator क्या है?
- &x और *p में क्या अंतर है?
- Pointer को initialize कैसे किया जाता है?
- Null pointer क्या है?
- Pointer और array के बीच क्या संबंध है?
- Pointer arithmetic क्या है?
- Pointer का उपयोग functions में क्यों किया जाता है?
- Pointer to Pointer क्या है?
- Pointer की सहायता से variable की value बदलने का program लिखिए।
- Pointer की सहायता से array के elements display करने का program लिखिए।
- Function में pointer argument pass करने का program लिखिए।
- Pointer और सामान्य variable में अंतर समझाइए।