Introduction
Pointer एक ऐसा variable होता है जो memory address store करता है।
अब सिर्फ pointer declare करना ही important नहीं है, बल्कि यह समझना भी जरूरी है कि pointer पर कौन-कौन से operations किए जा सकते हैं।
C language में pointer arithmetic बहुत powerful concept है, जिससे हम:
- memory locations को access कर सकते हैं
- arrays को efficiently process कर सकते हैं
- data traversal कर सकते हैं
लेकिन pointer operations थोड़े tricky होते हैं, इसलिए इन्हें ध्यान से समझना जरूरी है।
Definition
Pointer Operations वे operations होते हैं जो pointer variables पर perform किए जाते हैं, जैसे addition, subtraction और comparison।
Types of Pointer Operations
1. Pointer Arithmetic (Addition)
Pointer में number add करने पर वह next memory location पर move करता है
Example 1
#include <stdio.h>
int main()
{
int a = 10;
int *p = &a;
printf("%p\n", p);
printf("%p", p + 1);
return 0;
}
Explanation:
p→ address of ap + 1→ next integer location
Note:
- int = 4 bytes → address 4 bytes आगे बढ़ता है
2. Pointer Arithmetic (Subtraction)
Example 2
#include <stdio.h>
int main()
{
int a = 10;
int *p = &a;
printf("%p\n", p);
printf("%p", p - 1);
return 0;
}
Explanation:
- pointer पीछे move करता है
3. Pointer Difference
Example 3
#include <stdio.h>
int main()
{
int arr[5] = {1, 2, 3, 4, 5};
int *p1 = &arr[1];
int *p2 = &arr[4];
printf("%ld", p2 - p1);
return 0;
}
Output:
3
Explanation:
- index difference = 4 – 1 = 3
4. Pointer Comparison
Example 4
#include <stdio.h>
int main()
{
int a = 10, b = 20;
int *p1 = &a;
int *p2 = &b;
if (p1 == p2)
printf("Same");
else
printf("Different");
return 0;
}
Output:
Different
Explanation:
- दोनों अलग memory locations हैं
5. Pointer Increment / Decrement
Example 5
#include <stdio.h>
int main()
{
int arr[3] = {10, 20, 30};
int *p = arr;
printf("%d\n", *p);
p++;
printf("%d\n", *p);
p++;
printf("%d", *p);
return 0;
}
Output:
10
20
30
Explanation:
- pointer next elements पर move करता है
Important Rules
- Pointer addition/subtraction allowed है
- Multiplication/division allowed नहीं है
- Pointer arithmetic data type पर depend करता है
Technical Understanding
Pointer operations का use:
- array traversal
- memory navigation
- data structures (linked list)
- low-level programming
Important Points
- Pointer arithmetic memory address पर होता है
- Data type size important होता है
- pointer increment → next element
- pointer difference → index difference
Exam Points
- Array traversal में use होता है
- Pointer +1 → next memory location
- Pointer subtraction → difference देता है
- Comparison possible है
- Multiplication allowed नहीं है