0
0
C++programming~10 mins

Pointer arithmetic in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Pointer arithmetic
Pointer points to array start
Add integer to pointer
Pointer moves by integer * sizeof(type)
Pointer points to new element
Dereference pointer to access value
Use or modify value
Repeat or end
Pointer arithmetic moves the pointer by multiples of the data type size to access array elements.
Execution Sample
C++
int arr[3] = {10, 20, 30};
int* p = arr;
int val1 = *p;
p = p + 1;
int val2 = *p;
This code shows how a pointer moves through an integer array and accesses values.
Execution Table
StepPointer pExpressionPointer AddressDereferenced ValueAction
1p = arrp points to arr[0]Address of arr[0]10Pointer initialized to start of array
2val1 = *pDereference pAddress of arr[0]10Value 10 read from arr[0]
3p = p + 1Pointer arithmeticAddress of arr[1]20Pointer moved to next element
4val2 = *pDereference pAddress of arr[1]20Value 20 read from arr[1]
5End---Execution ends
💡 Pointer moved beyond array or program ends after accessing required elements
Variable Tracker
VariableStartAfter Step 1After Step 3Final
pundefinedAddress of arr[0]Address of arr[1]Address of arr[1]
val1undefined101010
val2undefinedundefinedundefined20
Key Moments - 2 Insights
Why does adding 1 to the pointer move it to the next array element, not just the next byte?
Pointer arithmetic moves the pointer by the size of the data type it points to. So adding 1 moves it by sizeof(int) bytes, pointing to the next int in the array (see Step 3 in execution_table).
What happens if we dereference the pointer after moving it beyond the array?
Dereferencing a pointer beyond the array leads to undefined behavior and can cause errors. The execution_table stops before this happens to avoid such issues.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of val1 after Step 2?
A20
B10
C30
DUndefined
💡 Hint
Check the 'Dereferenced Value' column at Step 2 in execution_table.
At which step does the pointer p point to the second element of the array?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Pointer Address' column and see when p moves to arr[1].
If we change the array to double type, how does pointer arithmetic change?
APointer moves by sizeof(int) bytes per increment
BPointer moves by 1 byte per increment
CPointer moves by sizeof(double) bytes per increment
DPointer does not move
💡 Hint
Pointer arithmetic depends on the data type size it points to, as explained in key_moments.
Concept Snapshot
Pointer arithmetic moves pointers by multiples of the data type size.
Adding 1 to a pointer moves it to the next element.
Dereferencing accesses the value at the pointer.
Use carefully to avoid going out of array bounds.
Full Transcript
Pointer arithmetic in C++ lets you move a pointer through an array by adding integers. Each addition moves the pointer by the size of the data type it points to. For example, adding 1 to an int pointer moves it by 4 bytes (usually) to the next int. You can then dereference the pointer to read or write the value at that position. The example code starts with a pointer to the first element of an int array, reads the value, moves the pointer to the next element, and reads again. The execution table shows each step with pointer address and values. Remember, going beyond the array is unsafe. This visual helps you see how pointer arithmetic works step-by-step.