- [20m] ☀️ Warm Up: Deep Thoughts on Variables
- [30m] 💬 TT: Pointers
- [10m] 🌴 BREAK
- [30m] 💻 Activity: Pointer Drills
- What is a variable?
- What part(s) of a variable are stored?
- Where are these things stored in the computer?
- Predict the output of the code snippet below. Justify your answer.
func zero(x int) {
x = 0
}
func main() {
x := 5
zero(x)
fmt.Println(x)
}
We use pointers because it's easier to give someone an address to your home than to give a copy of your home to everyone.
– Rishi Dua Oct 4 '13 at 14:51
- Imagine a sequence of boxes, placed one after another in a line.
- Each box is labeled with a unique number, which increments sequentially; this is the address of the box, its memory location.
- Each box can fit only one value.
- That value can be an integer, or a string, or an instance of a
struct
with 100 defined fields. - If you know the memory address of a box, you can go to that box and see what's inside.
- You can place a new value in that box; anything that was inside the box vanishes and is replaced by the new value.
- Pointers are a way of getting an indirect reference to another variable.
- Variables hold values. Pointers hold the address where the variable's values are stored in memory.
- Pointers allow us to access or modify the original value of a variable from anywhere in the program.
Pointers exist all over the place in the real world.
A great example are links on any web page!
A link on any page points to another page on the internet. When you copy the link and paste it into another web page, both links will point to the same, original web page. If the webmaster modifies that web page, the links will yield the new, updated page.
- Pointers are important in many data structures whose design requires the ability to link or chain one "node" to another efficiently. You would not "choose" a pointer over say a normal data type like float, they simply have different purposes.
- Pointers are useful where you require high performance and/or compact memory footprint.
- Pointers are declared using an asterisk (
*
) followed by the type of the stored value. - An asterisk is also used to dereference pointer values. Dereferencing a pointer returns the value stored at the address the pointer is holding.
- The
&
operator finds the address of a variable.
Pointers can be declared for addresses with primitive types, as well as structs, slices, maps, functions, and even other pointers!
package main
import "fmt"
func main() {
var x int = 5748
// declare pointer
var p *int
// initialize pointer
p = &x
// displaying the result
fmt.Println("Value stored in x = ", x)
fmt.Println("Address of x = ", &x)
fmt.Println("Value stored in variable p = ", p)
QUESTION: Comment out the line where the pointer is initialized, then run the program again. What is the value stored in p
?
When we call any function with an argument in Golang, that argument is copied to the function. The zero
function cannot modify the original x
variable declared in the main
function, so x
still equals 5
.
What if we wanted to modify the original variable?
We could use a pointer!
func zero(xPtr *int) {
*xPtr = 0
}
func main() {
x := 5
zero(&x)
fmt.Println(x) // x is 0
}
Visit Gradescope and begin working on Drill 3: Pointers
.