Address and dereference operators - Time & Space Complexity
We want to see how using address and dereference operators affects how long a program takes to run.
Specifically, we ask: does accessing memory with these operators slow down the program as input grows?
Analyze the time complexity of the following code snippet.
int sumArray(int *arr, int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += *(arr + i); // dereference operator
}
return sum;
}
This code sums all elements in an integer array using the address and dereference operators.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing each array element by dereferencing a pointer.
- How many times: Exactly once for each element, so n times.
As the array size grows, the program does more work by accessing more elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 dereferences and additions |
| 100 | 100 dereferences and additions |
| 1000 | 1000 dereferences and additions |
Pattern observation: The work grows directly with the number of elements; doubling n doubles the work.
Time Complexity: O(n)
This means the time to run grows in a straight line with the size of the input array.
[X] Wrong: "Using the dereference operator makes the code slower by a lot compared to normal array access."
[OK] Correct: Dereferencing a pointer is essentially how array access works under the hood, so it does not add extra time complexity.
Understanding how pointer access scales helps you explain memory operations clearly and shows you grasp how programs handle data step-by-step.
"What if we replaced the for-loop with recursion to sum the array? How would the time complexity change?"
Practice
& operator do in C?Solution
Step 1: Understand the
The&operator&operator is used to get the address of a variable in memory.Step 2: Differentiate from other operators
It does not multiply, dereference, or declare pointers; those are different operations.Final Answer:
It gives the memory address of a variable. -> Option AQuick Check:
&means address-of [OK]
- Confusing & with multiplication operator
- Thinking & dereferences a pointer
- Mixing & with pointer declaration
Solution
Step 1: Recall pointer declaration syntax
In C, a pointer to an int is declared asint *ptr;where*indicates pointer type.Step 2: Check other options for syntax errors
int &ptr;is invalid in C (it's C++ reference),int ptr*;and*int ptr;are incorrect syntax.Final Answer:
int *ptr; -> Option DQuick Check:
Pointer declaration uses * before variable name [OK]
- Using & instead of * for pointers
- Placing * after variable name incorrectly
- Confusing C++ references with pointers
int x = 10;
int *p = &x;
printf("%d", *p);Solution
Step 1: Understand pointer assignment
Pointerpstores the address ofxusing&x.Step 2: Dereference pointer to get value
*paccesses the value at the address stored inp, which is 10.Final Answer:
10 -> Option AQuick Check:
*p gives value at address [OK]
- Printing pointer variable instead of dereferenced value
- Confusing & and * operators
- Expecting address instead of value
int x = 5;
int *p;
*p = x;
printf("%d", *p);Solution
Step 1: Check pointer initialization
Pointerpis declared but not assigned an address before*p = x;.Step 2: Understand dereferencing uninitialized pointer
Dereferencingpwithout initialization causes undefined behavior or crash.Final Answer:
Pointer p is not initialized before dereferencing. -> Option BQuick Check:
Always assign pointer before dereferencing [OK]
- Dereferencing uninitialized pointer
- Confusing assignment with address-of
- Ignoring pointer initialization warnings
int a = 3, b = 7;
int *p = &a;
*p = *p + 4;
p = &b;
*p = *p - 2;
printf("%d %d", a, b);Solution
Step 1: Modify value pointed by p initially
ppoints toa.*p = *p + 4;changesafrom 3 to 7.Step 2: Change pointer to point to b and modify
p = &b;points tob.*p = *p - 2;changesbfrom 7 to 5.Step 3: Print final values
ais 7 andbis 5.Final Answer:
7 5 -> Option CQuick Check:
Pointer changes values via dereference [OK]
- Not updating the correct variable via pointer
- Confusing pointer assignment with value assignment
- Ignoring pointer reassignment effects
Ready to prove it?
Pass the test and this pattern moves unknown
