Pointer declaration in Go - Time & Space Complexity
When we declare pointers in Go, it's important to see how this affects the program's speed.
We want to know how the time to run changes as we use pointers.
Analyze the time complexity of the following code snippet.
package main
func increment(val *int) {
*val = *val + 1
}
func main() {
x := 5
increment(&x)
}
This code declares a pointer to an integer and uses it to increase the value by one.
Look for any repeated actions that take time.
- Primary operation: Single pointer dereference and increment.
- How many times: Happens once in this example.
Since the code runs a single operation, the time does not grow with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays the same no matter the input size.
Time Complexity: O(1)
This means the operation takes the same amount of time no matter what.
[X] Wrong: "Using pointers makes the program slower because it adds extra steps."
[OK] Correct: Accessing a pointer is just one quick step and does not slow down the program noticeably for simple operations.
Understanding how pointers work and their time cost helps you write clear and efficient code, a skill valued in many programming tasks.
"What if the increment function was called inside a loop running n times? How would the time complexity change?"
Practice
Solution
Step 1: Understand what a pointer represents
A pointer does not hold the value itself but the location where the value is stored in memory.Step 2: Recall pointer definition in Go
In Go, a pointer stores the address of a variable, not the variable's value directly.Final Answer:
The memory address of a variable -> Option BQuick Check:
Pointer = memory address [OK]
- Thinking pointers store the actual value
- Confusing pointer with variable type
- Assuming pointer stores variable size
num in Go?Solution
Step 1: Understand pointer declaration syntax
In Go, to declare a pointer to an int, use*intas the type.Step 2: Assign the address of
The address operatornumto the pointer&gets the memory address ofnum, which can be assigned to the pointer.Final Answer:
var p *int = &num -> Option CQuick Check:
Pointer declaration uses *type and &variable [OK]
- Forgetting the * in pointer type
- Assigning variable value instead of address
- Using *num instead of &num for assignment
package main
import "fmt"
func main() {
x := 10
p := &x
*p = 20
fmt.Println(x)
}Solution
Step 1: Understand pointer assignment and dereferencing
Variablepstores the address ofx. Using*p = 20changes the value at that address.Step 2: Check the effect on
Sincexppoints tox, changing*pchangesxto 20.Final Answer:
20 -> Option AQuick Check:
Dereferencing pointer changes original variable [OK]
- Thinking *p creates a new variable
- Expecting output to be address instead of value
- Confusing pointer declaration with dereferencing
var a int = 5 var p *int = a fmt.Println(*p)
Solution
Step 1: Check pointer assignment rules
Pointer variables must be assigned the address of a variable, not the variable's value.Step 2: Identify missing & operator
Assigningadirectly topis invalid; it should bep = &a.Final Answer:
Cannot assign int to *int without & operator -> Option AQuick Check:
Pointer assignment needs &variable [OK]
- Assigning value instead of address to pointer
- Forgetting to import fmt for Println
- Thinking *p prints address instead of value
package main
import "fmt"
func updateValue(p *int) {
*p = *p + 5
}
func main() {
val := 10
updateValue(&val)
fmt.Println(val)
}Solution
Step 1: Understand function parameter as pointer
The functionupdateValuetakes a pointer to an int and modifies the value at that address.Step 2: Trace the function call and modification
Passing&valsends the address ofval. Inside the function,*p = *p + 5increasesvalby 5.Step 3: Check final print output
After the function call,valis updated to 15, sofmt.Println(val)prints 15.Final Answer:
15 -> Option DQuick Check:
Pointer parameter modifies original variable [OK]
- Passing value instead of address to function
- Expecting original variable unchanged
- Confusing pointer dereference syntax
Ready to prove it?
Pass the test and this pattern moves unknown
