Pointers in C++
What is a pointer?
Every variable lives at some address in memory. A pointer is a variable that stores such an address instead of a normal value. In other words, a pointer "points to" where another variable lives.
Declaring a pointer
Put a * between the type and the name. The type says what kind of variable it points to:
int x = 42; int *p = &x; // p holds the address of x
Address-of and dereference
| Operator | Meaning |
|---|---|
&x | Address-of: the memory address of x |
*p | Dereference: the value stored at the address in p |
So p = &x makes p point to x, and *p reads or changes x through that pointer.
A working example
#include <iostream>
using namespace std;
int main() {
int x = 42;
int *p = &x;
cout << "x = " << x << "\n";
cout << "*p = " << *p << "\n"; // value via pointer
*p = 100; // change x through p
cout << "x now = " << x << "\n";
return 0;
}x = 42
*p = 42
x now = 100
nullptr
A pointer that points to nothing should be set to nullptr, and checked before use:
int *p = nullptr; if (p != nullptr) cout << *p; // safe: skips if null
Dereferencing a null or invalid pointer crashes the program, so this check matters.
Pointers and functions
Passing a pointer lets a function change the caller's variable (like pass-by-reference):
void addOne(int *p) { (*p)++; }
int main() {
int n = 5;
addOne(&n);
cout << n; // 6
}6
Pointers and arrays
An array's name acts like a pointer to its first element, so pointers can walk an array:
int a[3] = {10, 20, 30};
int *p = a; // points to a[0]
cout << *p << " " << *(p + 1); // 10 20Common mistakes
- Dereferencing an uninitialised or null pointer — a crash.
- Using a dangling pointer after its memory is freed or out of scope.
- Confusing
*p(the value) withp(the address). - Forgetting the parentheses in
(*p)++vs*p++.
Declare an int, make a pointer to it, print the value both directly and via the pointer, then change the value through the pointer. Write a swap(int*, int*) function that swaps two numbers.
Summary
- A pointer stores the address of another variable.
&gets an address;*dereferences to the value.- Set unused pointers to
nullptrand check before dereferencing. - Pointers let functions modify the caller's data and walk arrays.
- For everyday tasks, references are often safer than raw pointers.
Pointer क्या है?
हर variable memory में किसी address पर रहता है। Pointer एक variable है जो सामान्य value के बजाय ऐसा address रखता है। दूसरे शब्दों में, pointer "इशारा करता है" कि कोई अन्य variable कहाँ रहता है।
Pointer declare करना
Type और नाम के बीच एक * रखें। Type बताता है कि यह किस तरह के variable पर इशारा करता है:
int x = 42; int *p = &x; // p, x ka address rakhta hai
Address-of और dereference
| Operator | अर्थ |
|---|---|
&x | Address-of: x का memory address |
*p | Dereference: p के address पर रखी value |
तो p = &x p को x पर इशारा कराता है, और *p उस pointer के ज़रिए x पढ़ता या बदलता है।
एक चलता उदाहरण
#include <iostream>
using namespace std;
int main() {
int x = 42;
int *p = &x;
cout << "x = " << x << "\n";
cout << "*p = " << *p << "\n"; // pointer se value
*p = 100; // p ke zariye x badlein
cout << "x now = " << x << "\n";
return 0;
}x = 42
*p = 42
x now = 100
nullptr
जो pointer किसी चीज़ पर इशारा न करे उसे nullptr पर सेट करना चाहिए, और इस्तेमाल से पहले जाँचना चाहिए:
int *p = nullptr; if (p != nullptr) cout << *p; // safe: null hone par skip
Null या अमान्य pointer dereference करना program crash करता है, तो यह जाँच मायने रखती है।
Pointers और functions
Pointer भेजना किसी function को caller का variable बदलने देता है (pass-by-reference जैसा):
void addOne(int *p) { (*p)++; }
int main() {
int n = 5;
addOne(&n);
cout << n; // 6
}6
Pointers और arrays
Array का नाम उसके पहले element पर pointer की तरह काम करता है, तो pointers array में चल सकते हैं:
int a[3] = {10, 20, 30};
int *p = a; // a[0] par ishaara
cout << *p << " " << *(p + 1); // 10 20आम गलतियाँ
- Uninitialised या null pointer dereference करना — crash।
- Memory मुक्त होने या scope से बाहर जाने के बाद dangling pointer इस्तेमाल करना।
*p(value) कोp(address) से भ्रमित करना।(*p)++बनाम*p++में कोष्ठक भूलना।
एक int declare करें, उस पर pointer बनाएँ, value को सीधे और pointer के ज़रिए दोनों तरह print करें, फिर pointer के ज़रिए value बदलें। एक swap(int*, int*) function लिखें जो दो numbers की अदला-बदली करे।
सारांश
- Pointer किसी अन्य variable का address रखता है।
&address लेता है;*value तक dereference करता है।- अप्रयुक्त pointers को
nullptrपर सेट करें और dereference से पहले जाँचें। - Pointers functions को caller का data बदलने और arrays में चलने देते हैं।
- रोज़मर्रा के कामों के लिए references अक्सर raw pointers से सुरक्षित हैं।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C++ में pointer क्या है?
* से declare करते हैं, जैसे int *p;, और यह आपको उस variable को उसके address के ज़रिए पढ़ने या बदलने देता है जिसकी ओर यह इशारा करता है।C++ में * और & operators में क्या अंतर है?
& address-of operator किसी variable का memory address देता है, तो &x "x का address" है। * dereference operator pointer के रखे address पर जाकर वहाँ की value access करता है, तो *p "जिस पर p इशारा करता है वह value" है।C++ में nullptr क्या है?
nullptr एक विशेष value है जिसका अर्थ है कि pointer किसी चीज़ पर इशारा नहीं करता। यह पुराने NULL का आधुनिक, type-safe विकल्प है, और pointer इस्तेमाल करने से पहले if (p != nullptr) जाँचना अमान्य address dereference करने से होने वाले crashes से बचने में मदद करता है।C++ में dangling pointer क्या है?
nullptr पर सेट करना चाहिए।C++ में pointer और reference में क्या अंतर है?
* चाहिए। Reference किसी मौजूदा variable का उपनाम है, null या reseat नहीं हो सकता, और variable की तरह ही इस्तेमाल होता है। अधिकांश रोज़मर्रा के कामों के लिए references सुरक्षित हैं।