06 Pointers (Memory Magic)
Normal variable value store karta hai. Pointer variable doosre variable ka address (memory location) store karta hai.
1. The Syntax (* and &) 🔗
&(Address-of Operator): Kisi variable ka address nikalta hai.*(Value-at-Address Operator): Address par rakhi value nikalta hai (Dereferencing).
int a = 10;int *ptr = &a; // 'ptr' stores address of 'a'
printf("%p", ptr); // Prints address (e.g., 0x7ffee...)printf("%d", *ptr); // Prints value at that address (10)2. Why Pointers? 🤔
- Direct Memory Access: Hardware programming ke liye.
- Call by Reference: Functions me original value change karne ke liye.
- Dynamic Memory:
mallocaurcallocuse karne ke liye.
3. Pointer Arithmetic 🧮
Pointer + 1 ka matlab “Agla Address” (Bytes me jump).
Agar int pointer hai (4 bytes), to ptr + 1 4 bytes aage jayega.
int arr[] = {10, 20, 30};int *p = arr;
printf("%d", *p); // 10printf("%d", *(p + 1)); // 20 (Agla element)4. Double Pointer (**) 🤯
Pointer ka bhi address ho sakta hai!
int a = 5;int *p = &a;int **q = &p; // Pointer to a Pointer w