Pointers to pointers in C - Time & Space Complexity
We want to understand how the time it takes to run code with pointers to pointers changes as the input size grows.
Specifically, we ask: how many steps does the program take when it uses pointers to pointers?
Analyze the time complexity of the following code snippet.
void updateValues(int **ptr, int n) {
for (int i = 0; i < n; i++) {
*(*ptr + i) = i * 2;
}
}
This code updates an array of integers using a pointer to a pointer, setting each element to twice its index.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that updates each element in the array.
- How many times: It runs exactly n times, once for each element.
As the array size n grows, the number of steps grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 updates |
| 100 | 100 updates |
| 1000 | 1000 updates |
Pattern observation: The work grows directly with n; doubling n doubles the work.
Time Complexity: O(n)
This means the time to run the code grows in a straight line with the size of the input array.
[X] Wrong: "Using pointers to pointers makes the code slower and more complex, so it must increase time complexity."
[OK] Correct: The pointer type does not change how many times the loop runs; it only changes how we access memory. The time depends on the loop, not the pointer level.
Understanding how pointers to pointers affect time helps you explain memory access clearly and shows you can analyze code beyond just syntax.
"What if we added a nested loop inside the existing loop that also runs n times? How would the time complexity change?"
Practice
Solution
Step 1: Understand pointer basics
A pointer stores the address of a variable.Step 2: Understand pointer to pointer
A pointer to pointer stores the address of another pointer, not a direct value.Final Answer:
The address of another pointer -> Option CQuick Check:
Pointer to pointer = address of pointer [OK]
- Confusing pointer to pointer with pointer to value
- Thinking it stores the value directly
- Mixing it with function pointers
Solution
Step 1: Recall pointer declaration syntax
Single pointer to int is declared asint *ptr;.Step 2: Extend to pointer to pointer
Pointer to pointer uses two asterisks:int **ptr;.Final Answer:
int **ptr; -> Option AQuick Check:
Pointer to pointer declaration uses two * [OK]
- Placing * incorrectly in declaration
- Using & instead of *
- Confusing pointer syntax with references
int x = 10;
int *p = &x;
int **pp = &p;
printf("%d", **pp);Solution
Step 1: Understand variable assignments
x = 10, p points to x, pp points to p.Step 2: Evaluate **pp
*pp gives p (pointer to x), **pp gives value at x, which is 10.Final Answer:
10 -> Option DQuick Check:
**pp dereferences twice to get x's value [OK]
- Printing pointer address instead of value
- Using single * instead of **
- Confusing *p and **pp
int x = 5;
int *p = &x;
int **pp;
*pp = p;
printf("%d", **pp);Solution
Step 1: Check pointer initialization
pp is declared but not initialized to point anywhere.Step 2: Analyze dereferencing *pp = p;
Dereferencing uninitialized pp causes undefined behavior or crash.Final Answer:
pp is not initialized before dereferencing -> Option BQuick Check:
Dereferencing uninitialized pointer causes error [OK]
- Assuming pointer auto-initializes
- Ignoring uninitialized pointer usage
- Confusing pointer assignment with value assignment
int a = 3, b = 7;
int *p1 = &a;
int *p2 = &b;
int **pp = &p1;
*pp = p2;
printf("%d %d", *p1, *p2);Solution
Step 1: Initial pointer assignments
p1 points to a (3), p2 points to b (7), pp points to p1.Step 2: Change p1 via pointer to pointer
*pp = p2 changes p1 to point to b (7).Step 3: Print values
*p1 now points to b (7), *p2 is 7, so output is "7 7".Final Answer:
7 7 -> Option AQuick Check:
Pointer to pointer changed p1 to point to b [OK]
- Assuming p1 still points to a
- Confusing *pp and **pp
- Ignoring pointer reassignment effect
Ready to prove it?
Pass the test and this pattern moves unknown
