Pointer arithmetic lets you move through memory locations easily by adding or subtracting numbers to pointers.
Pointer arithmetic
pointer + n // moves pointer forward by n elements pointer - n // moves pointer backward by n elements pointer++ // moves pointer to next element pointer-- // moves pointer to previous element pointer1 - pointer2 // gives number of elements between two pointers
Pointer arithmetic moves in units of the size of the data type the pointer points to.
Adding 1 to a pointer moves it to the next element, not just the next byte.
int *p = arr; p = p + 2;
char *c = str;
c++;int diff = p2 - p1;This program shows how to move a pointer through an integer array and access different elements using pointer arithmetic.
#include <stdio.h> int main() { int arr[] = {10, 20, 30, 40, 50}; int *p = arr; // points to first element printf("First element: %d\n", *p); p = p + 3; // move pointer 3 elements ahead printf("Fourth element: %d\n", *p); p--; printf("Third element after moving back: %d\n", *p); int diff = p - arr; printf("Pointer is %d elements away from start\n", diff); return 0; }
Pointer arithmetic only works correctly with pointers pointing inside the same array or one past the end.
Be careful not to move pointers outside valid memory, or your program may crash.
Pointer arithmetic lets you move pointers by elements, not bytes.
You can add, subtract, increment, and decrement pointers.
Subtracting two pointers gives the number of elements between them.
Practice
int *p;Solution
Step 1: Understand pointer arithmetic basics
In C, adding 1 to a pointer moves it to the next element of the type it points to, not just 1 byte.Step 2: Apply to the given pointer
Sincepis anint*,p + 1points to the nextintin memory.Final Answer:
Moves the pointer to the next element of the type it points to -> Option DQuick Check:
Pointer + 1 moves by element size [OK]
- Thinking pointer arithmetic moves by 1 byte
- Confusing pointer increment with value increment
- Assuming pointer addition causes errors
int arr[5]; int *p = arr;
Solution
Step 1: Review pointer arithmetic syntax
Valid pointer arithmetic usesp + norp - nwhere n is an integer.Step 2: Check each option
p + 2 is valid syntax. Options A, C, and D contain invalid operators or spacing causing syntax errors.Final Answer:
p + 2 -> Option AQuick Check:
Correct pointer addition syntax isp + n[OK]
- Using invalid operators like '++' incorrectly
- Adding spaces that change meaning
- Confusing pointer increment with addition
int arr[] = {10, 20, 30, 40};
int *p = arr;
printf("%d", *(p + 2));Solution
Step 1: Understand pointer and array relation
Pointerppoints toarr[0], which is 10.Step 2: Calculate pointer offset
*(p + 2)accesses the element 2 positions afterarr[0], which isarr[2]= 30.Final Answer:
30 -> Option CQuick Check:
Pointer + 2 points to third element = 30 [OK]
- Confusing index starting at 1 instead of 0
- Dereferencing pointer without offset
- Mixing pointer arithmetic with array indexing
int arr[3] = {1, 2, 3};
int *p = arr + 3;
printf("%d", *p);Solution
Step 1: Analyze pointer assignment
p = arr + 3points to one element past the last valid element (index 3 in a 3-element array).Step 2: Understand dereferencing invalid pointer
Dereferencing*phere causes undefined behavior because it accesses memory outside the array.Final Answer:
Pointer points beyond array bounds causing undefined behavior -> Option BQuick Check:
Pointer must not dereference beyond array end [OK]
- Assuming pointer can safely dereference past array end
- Confusing pointer arithmetic with array length
- Ignoring undefined behavior risks
int *p1 and int *p2 pointing inside the same array, what does p2 - p1 represent?Solution
Step 1: Understand pointer subtraction in C
Subtracting two pointers gives the number of elements of the pointer type between them, not bytes.Step 2: Apply to pointers inside the same array
Sincep1andp2point inside the same array,p2 - p1counts how many elements separate them.Final Answer:
The number of elements between the two pointers -> Option AQuick Check:
Pointer subtraction = element count difference [OK]
- Thinking subtraction returns byte difference
- Assuming pointer subtraction is invalid
- Confusing pointer subtraction with pointer addition
Ready to prove it?
Pass the test and this pattern moves unknown
