Lesson 10 — Intermediate

Pointers in C

Pointer C ki pehchaan hai — ye variable ka ADDRESS rakhta hai, value nahi. Is samajh se arrays, strings, memory aur functions ka asli khel khulta hai. Is lesson mein hum pointer ki bhasha samjhenge, swap wale classic se pass by reference dekhenge, aur wo galtiyan bhi jo crash laati hain.

⏱ 16 min• Intermediate• Functions

Pointer kya hai — & aur *

WHAT

Pointer ek variable hai jisme KISI DOOSRE variable ka address hota hai. & address deta hai, * (dereference) address se value nikaalta hai. Pointer ka type batata hai usme kis type ka address rakh sakte ho.

WHEN

Jab original variable ko function mein badalna ho, jab dynamic memory chahiye ho, jab array/string pass karna ho — pointer har jagah hai.

WHERE

Heap memory (malloc), function arguments (swap), arrays (decay), data structures (linked list — DSA) — pointer ki wajah se ye sab mumkin hain.

#include <stdio.h>
int main() {
    int x = 10;
    int *p = &x;          // p mein x ka address

    printf("x = %d\n", x);     // 10
    printf("*p = %d\n", *p);   // 10 (address se value)
    printf("p = %p\n", p);     // 0x7ffd... (address)

    *p = 25;              // x badal diya (dereference se)
    printf("x = %d\n", x);     // 25
    return 0;
}

Output:
x = 10
*p = 10
p = 0x7ffd4a2b
x = 25

Pointer arithmetic aur arrays

#include <stdio.h>
int main() {
    int a[5] = {10, 20, 30, 40, 50};
    int *p = a;              // array = uska base address

    printf("%d\n", *p);        // 10
    printf("%d\n", *(p + 2));  // 30 (do element aage)

    p++;
    printf("%d\n", *p);        // 20 (4 byte aage — int ka size)
    return 0;
}

Output:
10
30
20

Pointer badhne ka matlab hai apne type ke size se utne bytes aage. int *++ = 4 bytes, char *++ = 1 byte, double *++ = 8 bytes. p + 2 ka matlab p se 2 elements aage — 2 bytes nahi!

Yaad rakho: Array naam seedha uska address hai — isliye a aur &a[0] same hain. isi liye pointer se array ki tarah traverse ho jaata hai. Ye combination interview ka sabse poochha hua pair hai.

Pass by reference — swap ka classic

#include <stdio.h>

void swap(int *a, int *b) {
    int t = *a;
    *a = *b;
    *b = t;
}

int main() {
    int x = 5, y = 9;
    swap(&x, &y);            // ADDRESS bheja
    printf("%d %d\n", x, y); // 9 5 — badal gaya!
    return 0;
}

Output: 9 5

Yahi hai call by reference ka C wala jugaad: function ko address do (&x), andar se *se value pakdo. Function apne pointers ki madad se asli variables tak pahunch jaata hai.

swap(&x, &y) ka flow:
Call : a = &x, b = &y (dono pointers mein addresses)
*a   : x ki value (5)
*a = *b : x = 9
*b = t  : y = 5
Result : x = 9, y = 5  ✓

Bina & ke (swap(x, y)) : sirf copies — kuch nahi badlega

NULL, dangling aur pointer traps

// 1. Uninitialized pointer — garbage
int *p;         // kisi ka address nahi — abhi random
// *p = 5;      // CRASH/silently memory kharab!

// 2. NULL se safe rehna
int *p = NULL;          // kisi object ka address nahi
if (p != NULL) *p = 5;  // safe check

// 3. Pointer to pointer
int x = 5;
int *p = &x;
int **pp = &p;
printf("%d\n", **pp);    // 5 — do level dereference
Debug mantra: Program random crash kare toh pointer check karo — (1) NULL toh nahi, (2) initialize hua hai ya nahi, (3) us memory ko kisi ne free toh nahi kiya. 90% crashes isi mein se aate hain.

Exercise

Question: Variable ka address lene wala operator kaunsa hai? (& ya *)

Question: Address se value nikalne (dereference) wala operator kaunsa hai? (& ya *)

Question: swap() ko kaam karne ke liye function ko kya bhejna padta hai? (value ya address)

Question: int *p = NULL; aur kisi address ke bina *p = 5 likhne par kya hota hai — chalta hai ya crash?

Common mistakes

Desi example: Ghar ka address — pointer ka desi funda

Pointer ghar ka address hai, value ghar ka samaan. Address bataya (aur dost nahi), samaan badalne gaya — yahi *p = 5 hai:

text
x = ghar (value = 10)
p = x ka address (jaise "42, Gandhi Nagar")
*p = ghar ke andar jaakar samaan badalna

&x = "kahan hai ghar?"  → address
*p = "andar kya hai?"   → value

swap(x, y)      = bina address bheje — kuch nahi hua
swap(&x, &y)    = address bheja — kaam ho gaya
Exam mapping (TCS / GATE / Interviews)
  • TCS NQT: pointer output (*p, &x) — 1-2Q fixed aata hai.
  • GATE: pointer arithmetic, NULL/dangling, pointer-array relation deep questions.
  • Interview: swap with pointers = sabse common live-coding question.
  • Trap: *p++ vs (*p)++ vs *++p — precedence wale questions exam mein aksar.
Lesson complete?

Pointers clear — Module 2 complete! Ab Module 3 — Advanced C — structures, files, dynamic memory aur preprocessor.

Popular courses: Data Science Course in Lucknow · AI Course in Lucknow · Python Course in Lucknow · Power BI Course · Data Analytics Course