🔄
Pointers

Pointers & Functions

Call by Reference
💡 Normal function call ek photocopy bhejne jaisa hai (original safe rehta hai), pointer se call karna asli document bhejne jaisa hai (function usse directly badal sakta hai).

C mein sab kuch default se "pass by value" hota hai — function ko copy milti hai. Agar function ko original variable modify karna hai, uska address (pointer) pass karna padta hai — isse "pass by reference" jaisa effect milta hai.

Classic example: swap function. Agar tum do numbers ko normal parameters se swap karne ki koshish karo, kuch nahi hoga (sirf local copies swap hongi). Pointers se hi ye kaam karta hai.

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

int main() {
  int x = 5, y = 10;
  swap(&x, &y);
  printf("%d %d\n", x, y); // 10 5 — actually swapped!
  return 0;
}
🔄
Normal function call ek photocopy bhejne jaisa hai (original safe rehta hai), pointer se call karna asli document bhejne jaisa hai (function usse directly badal sakta hai).
1 / 2
⚡ Quick Recap
  • Pass by value = copy milti hai, original safe
  • Pointer pass karne se function original ko modify kar sakta hai
  • swap() function pointers ka classic use-case hai
Is page mein (2 subtopics)

Function se pointer return karna common hai, lekin ek bada trap hai: kabhi bhi local variable ka address return mat karo — jaise hi function khatam hota hai, uski local variables (stack par) invalid ho jaati hain, aur returned pointer ek "dangling pointer" ban jaata hai jo garbage memory ko point karta hai.

Safe patterns: static variable ka address return karo (poori program lifetime tak zinda rehti hai), ya heap par malloc() se allocate ki hui memory ka address (caller ko baad mein free() karna padega), ya caller se ek buffer pass karwa lo aur usme fill karo.

// GALAT — dangling pointer:
int* badFunction() {
  int local = 10;
  return &local;   // DANGER: local function khatam hote hi mar jaata hai
}

// SAHI — static ya malloc use karo:
int* goodFunction() {
  static int value = 10;  // poori program lifetime tak zinda
  return &value;
}
⚠️Common Mistake: Local variable ka address return karna ek bahut common beginner bug hai — compiler kabhi-kabhi warning deta hai ("returning address of local variable"), lekin program silently galat behave kar sakta hai bina crash hue turant.

const int *p ka matlab hai "jis value ko p point kar raha hai wo change nahi ho sakti" (lekin p khud kisi aur address ko point kar sakta hai). int * const p ka matlab ulta hai — "p hamesha same address point karega" (lekin us address ki value change ho sakti hai).

const int * const p dono restrictions ek saath — na p kisi aur cheez ko point kar sakta hai, na jis value ko point kar raha hai wo change ho sakti hai. Interview mein ye teeno variants ka farq bahut common poocha jaata hai.

int x = 10, y = 20;

const int *p1 = &x;
// *p1 = 5;      // ERROR: value change nahi kar sakte
p1 = &y;          // OK: pointer kisi aur ko point kar sakta hai

int * const p2 = &x;
*p2 = 5;          // OK: value change kar sakte hain
// p2 = &y;       // ERROR: pointer ka target fix hai

const int * const p3 = &x;
// dono operations invalid hain p3 ke liye
💡Tip: Yaad rakhne ka trick: const jis side par * ke hai, wahi cheez "constant" (unchangeable) hai. const int *p → value constant. int * const p → pointer constant.