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.
Frequently Asked Questions
What is a pointer in C++?
*, such as int *p;, and it lets you read or change the variable it points to through its address.What is the difference between the * and & operators in C++?
& address-of operator gives the memory address of a variable, so &x is "the address of x." The * dereference operator goes to the address a pointer holds and accesses the value there, so *p is "the value p points to."What is nullptr in C++?
nullptr is a special value meaning a pointer points to nothing. It is the modern, type-safe replacement for the older NULL, and checking if (p != nullptr) before using a pointer helps avoid crashes from dereferencing an invalid address.What is a dangling pointer in C++?
nullptr after freeing.What is the difference between a pointer and a reference in C++?
* to access the value. A reference is an alias for an existing variable, cannot be null or reseated, and is used like the variable itself. References are safer for most everyday tasks.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 से सुरक्षित हैं।