C में Pointer Arithmetic: p+1, p-1 और Pointer Subtraction
p+1 एक byte नहीं जोड़ता — sizeof(type) bytes जोड़ता है. Array examples के साथ pointers का increment, decrement, subtraction और comparison master करें.
The golden rule of pointer arithmetic
sizeof(pointed-type) bytes. For an int * (4-byte int), p + 1 jumps 4 bytes forward — to the next int, not the next byte.int *pi; /* pi + 1 → address + 4 (next int) */
char *pc; /* pc + 1 → address + 1 (next char) */
double *pd; /* pd + 1 → address + 8 (next double) */
Proof program
#include <stdio.h>
int main() {
int arr[3] = {10, 20, 30};
int *p = arr;
printf("p = %p value = %d\n", (void*)p, *p);
printf("p + 1 = %p value = %d\n", (void*)(p+1), *(p+1));
printf("p + 2 = %p value = %d\n", (void*)(p+2), *(p+2));
return 0;
}
Addresses jump by 4 each time (int = 4 bytes), and each jump lands exactly on the next array element. This is why *(p + i) and arr[i] are the same thing.
Allowed operations
| Operation | Example | Result |
|---|---|---|
| Add integer | p + 3 | Address of 3 elements ahead |
| Subtract integer | p - 2 | Address of 2 elements back |
| Increment / decrement | p++, p-- | Move one element |
| Pointer − pointer | q - p | Number of elements between them |
| Comparison | p < q | Valid within the same array |
p + q), multiplying or dividing pointers. These have no meaning for addresses.Pointer subtraction example
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *first = &arr[0];
int *last = &arr[4];
printf("Elements between: %ld\n", last - first); /* 4, not 16 */
return 0;
}
last - first gives 4 elements, even though the byte difference is 16 — pointer subtraction automatically divides by sizeof(int).
Classic use: traversing an array with a pointer
#include <stdio.h>
int main() {
int marks[5] = {78, 85, 92, 66, 74};
int sum = 0;
for (int *p = marks; p < marks + 5; p++)
sum += *p;
printf("Total = %d\n", sum);
return 0;
}
Pointer arithmetic का golden rule
sizeof(pointed-type) bytes जोड़ता है. int * (4-byte int) के लिए p + 1 चार bytes आगे jump करता है — अगले int पर, अगले byte पर नहीं.int *pi; /* pi + 1 → address + 4 (अगला int) */
char *pc; /* pc + 1 → address + 1 (अगला char) */
double *pd; /* pd + 1 → address + 8 (अगला double) */
Proof program
#include <stdio.h>
int main() {
int arr[3] = {10, 20, 30};
int *p = arr;
printf("p = %p value = %d\n", (void*)p, *p);
printf("p + 1 = %p value = %d\n", (void*)(p+1), *(p+1));
printf("p + 2 = %p value = %d\n", (void*)(p+2), *(p+2));
return 0;
}
Addresses हर बार 4 से बढ़ते हैं (int = 4 bytes), और हर jump exactly अगले array element पर पहुंचता है. इसीलिए *(p + i) और arr[i] एक ही चीज़ हैं.
Allowed operations
| Operation | Example | Result |
|---|---|---|
| Integer जोड़ना | p + 3 | 3 elements आगे का address |
| Integer घटाना | p - 2 | 2 elements पीछे का address |
| Increment / decrement | p++, p-- | एक element move |
| Pointer − pointer | q - p | दोनों के बीच elements की संख्या |
| Comparison | p < q | Same array के अंदर valid |
p + q), pointers की multiplication या division. Addresses के लिए इनका कोई मतलब नहीं.Pointer subtraction example
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *first = &arr[0];
int *last = &arr[4];
printf("Elements between: %ld\n", last - first); /* 4, 16 नहीं */
return 0;
}
last - first देता है 4 elements, जबकि byte difference 16 है — pointer subtraction अपने आप sizeof(int) से divide कर देता है.
Classic use: Pointer से array traverse करना
#include <stdio.h>
int main() {
int marks[5] = {78, 85, 92, 66, 74};
int sum = 0;
for (int *p = marks; p < marks + 5; p++)
sum += *p;
printf("Total = %d\n", sum);
return 0;
}
Frequently Asked Questions
Pointer arithmetic में p+1 का क्या मतलब है?
p+1 pointed type के अगले element का address देता है: यह sizeof(type) bytes जोड़ता है, इसलिए int* चार bytes और double* आठ bytes आगे जाता है.
दो pointers घटाने पर क्या मिलता है?
q - p दोनों pointers के बीच elements की संख्या देता है (byte difference को sizeof type से divide करके), जो सिर्फ same array में valid है.
Pointers पर कौन-सी arithmetic operations illegal हैं?
दो pointers जोड़ना, pointers की multiplication या division — सब illegal हैं; सिर्फ integer जोड़ना/घटाना, pointer subtraction और comparison meaningful हैं.