Bird
Raised Fist0
C++programming~10 mins

Pointers and arrays in C++ - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Pointers and arrays
Declare array
→Array memory allocated
↓
Declare pointer
→Pointer assigned to array start
↓
Access elements via pointer
↓
Pointer arithmetic to move through array
↓
Read or modify array elements
↓
End
This flow shows how an array is declared and how a pointer can be assigned to its first element. Then, the pointer moves through the array to access or change elements.
Execution Sample
C++
int arr[3] = {10, 20, 30};
int* p = arr;
int first = *p;
p++;
int second = *p;
This code declares an array and a pointer to its first element, then reads the first and second elements using the pointer.
Execution Table
StepActionPointer pValue pointed by p (*p)VariablesOutput
1Declare array arr with values {10, 20, 30}N/AN/Aarr = {10,20,30}N/A
2Declare pointer p and assign to arr (start of array)Address of arr[0]10p -> arr[0]N/A
3Read first element via *pAddress of arr[0]10first = 10first = 10
4Increment pointer p (p++)Address of arr[1]20p -> arr[1]N/A
5Read second element via *pAddress of arr[1]20second = 20second = 20
6End of executionAddress of arr[1]20first=10, second=20Program ends
💡 Pointer p has moved through array elements; program ends after reading second element.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
arr{10,20,30}{10,20,30}{10,20,30}{10,20,30}{10,20,30}{10,20,30}
pUninitializedPoints to arr[0]Points to arr[0]Points to arr[1]Points to arr[1]Points to arr[1]
firstUninitializedUninitialized10101010
secondUninitializedUninitializedUninitializedUninitialized2020
Key Moments - 3 Insights
Why does incrementing the pointer (p++) move to the next array element?
Because in C++, pointer arithmetic moves the pointer by the size of the data type it points to. Here, p points to int, so p++ moves to the next int in the array (next element). See execution_table step 4.
Why can we assign the array name 'arr' directly to the pointer 'p'?
Because the array name 'arr' represents the address of its first element. So assigning p = arr makes p point to arr[0]. See execution_table step 2.
What does *p mean when p is a pointer?
*p means accessing the value stored at the memory location p points to. So *p reads or writes the array element p points to. See execution_table steps 3 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What does pointer p point to after p++?
Aarr[2] (value 30)
Barr[0] (value 10)
Carr[1] (value 20)
DNull pointer
💡 Hint
Check the 'Pointer p' and '*p' columns at step 4 in execution_table.
At which step is the variable 'first' assigned a value?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Variables' column in execution_table to see when 'first' gets a value.
If we did not increment pointer p (no p++), what would 'second' be assigned at step 5?
A10
B20
CUndefined
D30
💡 Hint
Without p++ pointer stays at arr[0], so *p reads arr[0] value. See variable_tracker for pointer positions.
Concept Snapshot
Pointers and arrays in C++:
- Array name is address of first element
- Pointer can point to array start: int* p = arr;
- *p accesses value pointed by p
- p++ moves pointer to next element (by sizeof element)
- Use pointers to traverse or modify arrays efficiently
Full Transcript
This lesson shows how arrays and pointers work together in C++. We start by declaring an array of integers. The array name represents the address of its first element. We declare a pointer and assign it to the array name, so it points to the first element. Using *p, we read the value at the pointer. Incrementing the pointer with p++ moves it to the next element in the array. We then read the second element using *p again. The execution table tracks each step, showing pointer position and values accessed. Key moments clarify why pointer arithmetic works and what *p means. The quiz tests understanding of pointer movement and variable assignments. This helps beginners see how pointers and arrays connect in memory.

Practice

(1/5)
1. What does the name of an array represent in C++?
easy
A. A copy of the entire array
B. The total size of the array
C. The last element of the array
D. A pointer to the first element of the array

Solution

  1. Step 1: Understand array name behavior

    In C++, the name of an array acts as a pointer to its first element, not the whole array or size.
  2. Step 2: Confirm pointer usage

    This pointer can be used to access elements by pointer arithmetic or indexing.
  3. Final Answer:

    A pointer to the first element of the array -> Option D
  4. Quick Check:

    Array name = pointer to first element [OK]
Hint: Remember array name points to element zero [OK]
Common Mistakes:
  • Thinking array name stores size
  • Confusing array name with last element
  • Assuming array name copies data
2. Which of the following is the correct way to declare a pointer to an integer array named arr?
easy
A. int *ptr = &arr;
B. int ptr = &arr;
C. int *ptr = arr;
D. int ptr = arr;

Solution

  1. Step 1: Understand pointer declaration

    To declare a pointer to an integer, use int *ptr. The array name arr acts as a pointer to its first element.
  2. Step 2: Assign pointer correctly

    Assigning ptr = arr; sets the pointer to the first element's address. Using &arr gives the address of the whole array, which has a different type.
  3. Final Answer:

    int *ptr = arr; -> Option C
  4. Quick Check:

    Pointer to first element = int *ptr = arr [OK]
Hint: Use int *ptr = arr; to point to array start [OK]
Common Mistakes:
  • Using &arr instead of arr for pointer
  • Declaring pointer as int instead of int*
  • Assigning pointer without *
3. What is the output of the following code?
int arr[] = {10, 20, 30};
int *p = arr;
std::cout << *(p + 1) << std::endl;
medium
A. 20
B. 10
C. 30
D. Compilation error

Solution

  1. Step 1: Understand pointer arithmetic

    Pointer p points to arr[0]. Adding 1 moves pointer to arr[1].
  2. Step 2: Dereference pointer at new position

    Dereferencing *(p + 1) gives the value at arr[1], which is 20.
  3. Final Answer:

    20 -> Option A
  4. Quick Check:

    *(p + 1) = arr[1] = 20 [OK]
Hint: Pointer + 1 points to next array element [OK]
Common Mistakes:
  • Confusing pointer arithmetic with addition of values
  • Dereferencing pointer without offset
  • Expecting compilation error
4. Identify the error in the following code snippet:
int arr[3] = {1, 2, 3};
int *p = &arr;
std::cout << *(p + 2) << std::endl;
medium
A. Pointer declaration is correct, no error
B. Incorrect pointer assignment: &arr should be arr
C. Dereferencing pointer is invalid syntax
D. Array size is too small for pointer arithmetic

Solution

  1. Step 1: Analyze pointer assignment

    arr is an array name acting as pointer to first element. &arr is address of whole array, type int (*)[3], incompatible with int *.
  2. Step 2: Correct pointer assignment

    Assigning int *p = arr; is correct to point to first element. Using &arr causes type mismatch and incorrect pointer arithmetic.
  3. Final Answer:

    Incorrect pointer assignment: &arr should be arr -> Option B
  4. Quick Check:

    Use arr, not &arr, for int* pointer [OK]
Hint: Use arr, not &arr, when assigning int* pointer [OK]
Common Mistakes:
  • Using &arr instead of arr for int*
  • Ignoring pointer type mismatch warnings
  • Assuming &arr points to first element
5. Given the code below, what will be the output?
int arr[] = {5, 10, 15, 20};
int *p = arr + 1;
std::cout << p[2] << std::endl;
hard
A. 20
B. 15
C. Undefined behavior
D. 10

Solution

  1. Step 1: Understand pointer initialization

    p = arr + 1; points to arr[1], which is 10.
  2. Step 2: Access element using pointer indexing

    p[2] means *(p + 2), which points to arr[3] (1 + 2 = 3), value 20.
  3. Final Answer:

    20 -> Option A
  4. Quick Check:

    p[2] = arr[3] = 20 [OK]
Hint: Pointer[index] = *(pointer + index) [OK]
Common Mistakes:
  • Confusing p[2] with arr[2]
  • Forgetting pointer offset starts at arr + 1
  • Assuming p[2] is out of bounds
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