0
0
Cprogramming~10 mins

Pointer arithmetic - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Pointer arithmetic
Declare pointer p
Assign address of array element to p
Perform arithmetic: p + n
Pointer points to new element
Dereference *p to access value
Use or print value
End or repeat with new pointer
Pointer arithmetic moves the pointer to different elements in an array by adding or subtracting integers, then accessing the value at the new location.
Execution Sample
C
int arr[3] = {10, 20, 30};
int *p = arr;
int val1 = *p;
p += 1;
int val2 = *p;
p += 1;
int val3 = *p;
This code sets a pointer to the start of an array and accesses each element by moving the pointer with arithmetic.
Execution Table
StepPointer pExpressionDereferenced ValueExplanation
1arr (address of arr[0])*p10Pointer p points to arr[0], value 10 accessed
2arr + 1 (address of arr[1])*p20Pointer moved to arr[1], value 20 accessed
3arr + 2 (address of arr[2])*p30Pointer moved to arr[2], value 30 accessed
4arr + 3*pUndefined behaviorPointer moved beyond array bounds, accessing invalid memory
💡 Pointer arithmetic beyond array size leads to undefined behavior; execution stops after valid accesses.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
parr (address of arr[0])arrarr + 1arr + 2arr + 3
*pN/A102030Undefined behavior
Key Moments - 2 Insights
Why does *p cause a problem?
Because the array has only 3 elements (indices 0 to 2), *p points outside the array, causing undefined behavior as shown in execution_table row 4.
Does adding 1 to a pointer increase its address by 1 byte?
No, adding 1 to a pointer moves it to the next element of its type, so the address increases by the size of the element (e.g., 4 bytes for int), as seen in the pointer values in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what value does *p give at step 2?
A20
B30
C10
DUndefined
💡 Hint
Check the 'Dereferenced Value' column at step 2 in execution_table.
At which step does pointer arithmetic go beyond the array bounds?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Look at the 'Explanation' column in execution_table for the step where undefined behavior occurs.
If the array elements were of type char (1 byte), how would pointer arithmetic change?
APointer increments by 4 bytes
BPointer increments by 2 bytes
CPointer increments by 1 byte
DPointer does not increment
💡 Hint
Pointer arithmetic moves by the size of the data type; char is 1 byte.
Concept Snapshot
Pointer arithmetic in C:
- Pointer + n moves pointer by n elements (not bytes)
- *p accesses value at pointer
- Pointer must point within array bounds
- Going outside array causes undefined behavior
- Pointer arithmetic depends on data type size
Full Transcript
Pointer arithmetic lets you move a pointer through an array by adding numbers. When you add 1 to a pointer, it moves to the next element, not just one byte. For example, if you have an int array, adding 1 moves the pointer by 4 bytes (size of int). You can then use *p to get the value at the pointer. But be careful: if you move the pointer beyond the array size, you get undefined behavior, which can cause errors. The example code shows a pointer moving through an array of three ints, accessing each value correctly, then going out of bounds at the end.