🔵 Core C++  ·  Lesson 26

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:

C++
int x = 42;
int *p = &x;   // p holds the address of x

Address-of and dereference

OperatorMeaning
&xAddress-of: the memory address of x
*pDereference: 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

C++
#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;
}
Output:
x = 42
*p = 42
x now = 100

nullptr

A pointer that points to nothing should be set to nullptr, and checked before use:

C++
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):

C++
void addOne(int *p) { (*p)++; }
int main() {
    int n = 5;
    addOne(&n);
    cout << n;   // 6
}
Output:
6

Pointers and arrays

An array's name acts like a pointer to its first element, so pointers can walk an array:

C++
int a[3] = {10, 20, 30};
int *p = a;              // points to a[0]
cout << *p << " " << *(p + 1);   // 10 20

Common 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) with p (the address).
  • Forgetting the parentheses in (*p)++ vs *p++.
🏋️ Practice

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 nullptr and 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++?
A pointer is a variable that stores the memory address of another variable, rather than a value directly. You declare it with a *, 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++?
The & 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++?
A dangling pointer is one that still holds an address whose memory has been freed or has gone out of scope. Using it is undefined behaviour, because the memory may now hold something else or be invalid, so you should set such pointers to nullptr after freeing.
What is the difference between a pointer and a reference in C++?
A pointer is a variable holding an address that can be reassigned and can be null, and it needs * 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 पर इशारा करता है:

C++
int x = 42;
int *p = &x;   // p, x ka address rakhta hai

Address-of और dereference

Operatorअर्थ
&xAddress-of: x का memory address
*pDereference: p के address पर रखी value

तो p = &x p को x पर इशारा कराता है, और *p उस pointer के ज़रिए x पढ़ता या बदलता है।

एक चलता उदाहरण

C++
#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;
}
Output:
x = 42
*p = 42
x now = 100

nullptr

जो pointer किसी चीज़ पर इशारा न करे उसे nullptr पर सेट करना चाहिए, और इस्तेमाल से पहले जाँचना चाहिए:

C++
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 जैसा):

C++
void addOne(int *p) { (*p)++; }
int main() {
    int n = 5;
    addOne(&n);
    cout << n;   // 6
}
Output:
6

Pointers और arrays

Array का नाम उसके पहले element पर pointer की तरह काम करता है, तो pointers array में चल सकते हैं:

C++
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 से सुरक्षित हैं।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.