0
0
Cprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Pointers and arrays
Declare array
Array memory allocated
Pointer declared
Pointer assigned to array address
Access elements via pointer
Modify elements via pointer
Pointer arithmetic to move through array
End of access
This flow shows how an array is declared and how a pointer can be assigned to its address to access and modify elements using pointer arithmetic.
Execution Sample
C
int arr[3] = {10, 20, 30};
int *p = arr;
int val = *(p + 1);
This code declares an array, assigns a pointer to its start, and accesses the second element using pointer arithmetic.
Execution Table
StepActionPointer pExpressionValue AccessedArray State
1Declare array arr with values {10,20,30}N/AN/AN/A[10, 20, 30]
2Declare pointer pUninitializedN/AN/A[10, 20, 30]
3Assign p = arr (address of arr[0])Address of arr[0]N/AN/A[10, 20, 30]
4Access *(p + 1)Address of arr[0]*(p + 1)20[10, 20, 30]
5Modify *(p + 2) = 40Address of arr[0]*(p + 2) = 4040[10, 20, 40]
6Increment pointer p++Address of arr[1]p++N/A[10, 20, 40]
7Access *pAddress of arr[1]*p20[10, 20, 40]
8End of accessAddress of arr[1]N/AN/A[10, 20, 40]
💡 Pointer arithmetic and array access complete; program ends.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 6Final
arr[10, 20, 30][10, 20, 30][10, 20, 40][10, 20, 40][10, 20, 40]
pUninitializedAddress of arr[0]Address of arr[0]Address of arr[1]Address of arr[1]
valUndefinedUndefined202020
Key Moments - 3 Insights
Why does *(p + 1) access the second element of the array?
Because p points to the first element, adding 1 moves the pointer to the next element in memory, so *(p + 1) dereferences the second element. See execution_table row 4.
What happens when we do p++ on the pointer?
The pointer moves to the next element in the array (address of arr[1]). It does not change the array contents. See execution_table row 6.
How does modifying *(p + 2) affect the array?
It changes the value at the third element of the array because (p + 2) points to arr[2]. The array updates accordingly. See execution_table row 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what value does *(p + 1) access?
A10
B20
C30
D40
💡 Hint
Check the 'Value Accessed' column at step 4 in execution_table.
At which step does the array value change from 30 to 40?
AStep 6
BStep 4
CStep 5
DStep 7
💡 Hint
Look at the 'Array State' column in execution_table to see when the last element changes.
After p++, what element does *p point to?
Aarr[1]
Barr[0]
Carr[2]
DUndefined
💡 Hint
Check pointer p value in variable_tracker after step 6.
Concept Snapshot
Pointers and arrays:
- Array name is address of first element
- Pointer can point to array start
- *(p + i) accesses i-th element
- Pointer arithmetic moves by element size
- Modifying *p changes array content
- p++ moves pointer to next element
Full Transcript
This lesson shows how arrays and pointers work together in C. We start by declaring an array with three integers. Then we declare a pointer and assign it to the array's address, which points to the first element. Using pointer arithmetic, we access the second element with *(p + 1), which gives us 20. We also modify the third element by setting *(p + 2) to 40, changing the array's last value. Incrementing the pointer with p++ moves it to the second element. Accessing *p now gives 20. This step-by-step trace helps understand how pointers and arrays relate in memory.