Pointer Arithmetic in C: p+1, p-1 and Pointer Subtraction
p+1 does not add 1 byte — it adds sizeof(type) bytes. Master increment, decrement, subtraction and comparison of pointers with array examples.
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
What does p+1 mean in pointer arithmetic?
p+1 gives the address of the next element of the pointed type: it adds sizeof(type) bytes, so int* moves 4 bytes and double* moves 8.
What does subtracting two pointers give?
q - p gives the number of elements between the two pointers (byte difference divided by sizeof type), valid only within the same array.
Which arithmetic operations are illegal on pointers?
Adding two pointers, multiplying or dividing pointers are all illegal; only integer addition/subtraction, pointer subtraction and comparison are meaningful.