🔵 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.

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 से सुरक्षित हैं।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में pointer क्या है?
Pointer एक variable है जो सीधे value के बजाय किसी अन्य variable का memory address रखता है। आप इसे * से 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 क्या है?
Dangling pointer वह है जो अब भी ऐसा address रखता है जिसकी memory मुक्त कर दी गई या scope से बाहर चली गई। इसे इस्तेमाल करना undefined behaviour है, क्योंकि memory अब कुछ और रख सकती है या अमान्य हो सकती है, तो मुक्त करने के बाद ऐसे pointers को nullptr पर सेट करना चाहिए।
C++ में pointer और reference में क्या अंतर है?
Pointer एक address रखने वाला variable है जिसे फिर से assign किया जा सकता है और null हो सकता है, और value access करने को * चाहिए। Reference किसी मौजूदा variable का उपनाम है, null या reseat नहीं हो सकता, और variable की तरह ही इस्तेमाल होता है। अधिकांश रोज़मर्रा के कामों के लिए references सुरक्षित हैं।
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.