Bird
Raised Fist0
C++programming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does adding 1 to a pointer in C++ do when the pointer points to an int array?
easy
A. Moves the pointer to the next integer element in the array
B. Increases the pointer's memory address by 1 byte
C. Moves the pointer to the next byte in memory
D. Causes a compilation error

Solution

  1. Step 1: Understand pointer arithmetic basics

    Adding 1 to a pointer moves it to the next element of the type it points to, not just 1 byte.
  2. Step 2: Apply to int pointer

    Since int usually occupies 4 bytes, adding 1 moves the pointer by 4 bytes to the next integer.
  3. Final Answer:

    Moves the pointer to the next integer element in the array -> Option A
  4. Quick Check:

    Pointer + 1 = next element [OK]
Hint: Pointer + 1 moves to next element, not next byte [OK]
Common Mistakes:
  • Thinking pointer arithmetic adds 1 byte always
  • Confusing pointer increment with byte increment
  • Assuming pointer arithmetic causes errors
2. Which of the following pointer arithmetic expressions is syntactically correct in C++?
easy
A. 1 + ptr
B. All of the above
C. ptr - 1
D. ptr + 1

Solution

  1. Step 1: Check pointer addition syntax

    Adding an integer to a pointer like ptr + 1 is valid and moves the pointer forward.
  2. Step 2: Check commutativity and subtraction

    Integer + pointer like 1 + ptr is also valid due to commutativity. Pointer - integer like ptr - 1 moves pointer backward and is valid.
  3. Final Answer:

    All of the above -> Option B
  4. Quick Check:

    Pointer + int and int + pointer both valid [OK]
Hint: Pointer + int and int + pointer both work [OK]
Common Mistakes:
  • Assuming int + pointer is invalid
  • Thinking pointer - int is not allowed
  • Confusing pointer arithmetic with pointer subtraction
3. What is the output of the following C++ code?
int arr[] = {10, 20, 30, 40};
int *p = arr;
int *q = arr + 3;
std::cout << q - p << std::endl;
medium
A. 3
B. 4
C. 30
D. Compilation error

Solution

  1. Step 1: Understand pointer subtraction

    Subtracting two pointers gives the number of elements between them, not bytes.
  2. Step 2: Calculate difference between pointers

    q = arr + 3 points to the 4th element, p = arr points to the 1st. Difference is 3 elements.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Pointer difference = number of elements [OK]
Hint: Pointer subtraction returns element count, not bytes [OK]
Common Mistakes:
  • Thinking pointer subtraction returns byte difference
  • Confusing pointer difference with array values
  • Expecting compilation error
4. Identify the error in the following code snippet:
int arr[5] = {1, 2, 3, 4, 5};
int *p = arr + 5;
std::cout << *p << std::endl;
medium
A. Array size is incorrect
B. Syntax error in pointer initialization
C. Pointer points outside array bounds, causing undefined behavior
D. Dereferencing pointer is valid and prints 5

Solution

  1. Step 1: Analyze pointer initialization

    p = arr + 5 points one past the last element (index 5), which is outside the array bounds.
  2. Step 2: Check dereferencing pointer

    Dereferencing *p here is undefined behavior because pointer is out of valid range.
  3. Final Answer:

    Pointer points outside array bounds, causing undefined behavior -> Option C
  4. Quick Check:

    Dereferencing out-of-bounds pointer is unsafe [OK]
Hint: Pointer must not dereference beyond array end [OK]
Common Mistakes:
  • Assuming pointer at arr+5 is valid to dereference
  • Confusing pointer arithmetic with array indexing
  • Ignoring undefined behavior risks
5. Given the code below, what will be the output?
int arr[] = {5, 10, 15, 20, 25};
int *p = arr + 1;
int *q = arr + 4;
int diff = q - p;
std::cout << *(p + diff) << std::endl;
hard
A. Undefined behavior
B. 20
C. Compilation error
D. 25

Solution

  1. Step 1: Calculate pointer difference

    q - p = (arr + 4) - (arr + 1) = 3 elements difference.
  2. Step 2: Evaluate pointer arithmetic and dereference

    p + diff = (arr + 1) + 3 = arr + 4, which points to the element 25. Dereferencing gives 25.
  3. Final Answer:

    25 -> Option D
  4. Quick Check:

    Pointer arithmetic + dereference = correct element [OK]
Hint: Pointer difference + pointer = new pointer position [OK]
Common Mistakes:
  • Confusing pointer difference with value difference
  • Miscalculating pointer arithmetic
  • Assuming undefined behavior without reason
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard