Pointers and arrays in C - Time & Space Complexity
When working with pointers and arrays, it is important to understand how the program's running time changes as the size of the array grows.
We want to know how many steps the program takes when it accesses or processes array elements using pointers.
Analyze the time complexity of the following code snippet.
void printArray(int *arr, int size) {
for (int i = 0; i < size; i++) {
printf("%d ", *(arr + i));
}
printf("\n");
}
This code prints each element of an integer array using a pointer to access elements.
- Primary operation: Accessing each array element through pointer arithmetic inside the loop.
- How many times: The loop runs exactly size times, once for each element.
As the array size grows, the number of steps grows in a straight line with it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 pointer accesses and prints |
| 100 | About 100 pointer accesses and prints |
| 1000 | About 1000 pointer accesses and prints |
Pattern observation: The work grows evenly as the array gets bigger, doubling the size doubles the work.
Time Complexity: O(n)
This means the time to run the code grows directly in proportion to the number of elements in the array.
[X] Wrong: "Using pointers makes accessing array elements faster and changes the time complexity."
[OK] Correct: Pointer access and array indexing both take constant time per element, so the overall time still grows linearly with the array size.
Understanding how pointer and array access scales helps you explain memory handling and performance clearly in interviews.
"What if we used nested loops to process a 2D array with pointers? How would the time complexity change?"
Practice
Solution
Step 1: Understand pointer basics
A pointer stores a memory address, not values or counts.Step 2: Pointer to array points to first element
In C, a pointer to an array points to the first element's address.Final Answer:
The address of the first element of the array -> Option AQuick Check:
Pointer = first element address [OK]
- Thinking pointer stores array size
- Confusing pointer with array value
- Assuming pointer stores last element
Solution
Step 1: Recall pointer declaration syntax
In C, a pointer to int is declared as int *ptr;Step 2: Check other options for syntax errors
int ptr[] declares an array, not a pointer; int &ptr is invalid in C; int ptr* is incorrect syntax.Final Answer:
int *ptr; -> Option BQuick Check:
Pointer syntax uses * before variable [OK]
- Using array syntax instead of pointer
- Confusing C++ reference (&) with pointer
- Placing * after variable name incorrectly
int arr[] = {10, 20, 30};
int *p = arr;
printf("%d", *(p + 1));Solution
Step 1: Understand pointer arithmetic
p points to arr[0], so p + 1 points to arr[1].Step 2: Dereference pointer at p + 1
*(p + 1) gives value at arr[1], which is 20.Final Answer:
20 -> Option DQuick Check:
*(p+1) = arr[1] = 20 [OK]
- Confusing *(p+1) with *p
- Assuming pointer arithmetic adds 1 byte
- Expecting arr[0] value instead
int arr[3] = {1, 2, 3};
int *p = arr + 3;
printf("%d", *p);Solution
Step 1: Analyze pointer assignment
arr + 3 points one element past the last array element (out of bounds).Step 2: Dereferencing out-of-bounds pointer
*p accesses invalid memory, causing undefined behavior.Final Answer:
Pointer points outside array bounds causing undefined behavior -> Option CQuick Check:
Pointer out of array range = error [OK]
- Ignoring pointer out-of-bounds access
- Thinking array size is wrong
- Confusing pointer syntax errors
int arr[] = {5, 10, 15, 20};
int *p = arr;
for(int i = 0; i < 4; i++) {
printf("%d ", *(p + i));
}Solution
Step 1: Understand the loop and pointer arithmetic
Loop runs i from 0 to 3, *(p + i) accesses arr[i].Step 2: Print each element in order
Values printed are arr[0]=5, arr[1]=10, arr[2]=15, arr[3]=20.Final Answer:
5 10 15 20 -> Option AQuick Check:
Pointer + i accesses arr[i] in order [OK]
- Assuming reverse order print
- Printing same element repeatedly
- Expecting syntax error
Ready to prove it?
Pass the test and this pattern moves unknown
