Pointers let you store the address of a variable. This helps you change the original value from different places in your program.
Pointer declaration in Go
var p *int // or var p *Type
The asterisk (*) before the type means this variable holds a pointer to that type.
Pointer variables store memory addresses, not the actual value.
var p *intvar name *string
var p *float64
This program shows how to declare a pointer, assign it the address of a variable, and change the variable's value through the pointer.
package main import "fmt" func main() { var x int = 10 var p *int = &x // p holds the address of x fmt.Println("Value of x:", x) fmt.Println("Address of x stored in p:", p) fmt.Println("Value pointed to by p:", *p) *p = 20 // change value at address p points to fmt.Println("New value of x after change through p:", x) }
Use the ampersand (&) to get the address of a variable.
Use the asterisk (*) to get or set the value at the pointer's address.
Pointer addresses will look different each time you run the program.
Pointers store addresses of variables, not the actual values.
Use & to get a variable's address and * to access or change the value at that address.
Pointers help you work with data efficiently and allow functions to modify variables outside their scope.
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
