This feed contains pages with tag "pointers".
Getting started
- Make a directory
~/cs2613/labs/L19 - All of your work from this half of the lab should be saved to this directory, committed and pushed before the end of the lab.
Functions
- Time
- 25 minutes
- Activity
- Complete given example
- Save the following example (based on gobyexample/functions) to
~/cs2613/labs/L19/functions.go
package main
import "fmt"
// Here's a function that takes two `int`s and returns
// their sum as an `int`.
func plus(a int, b int) int {
// fill in a recursive definition of plus in the style of Lab 2
}
// When you have multiple consecutive parameters of
// the same type, you may omit the type name for the
// like-typed parameters up to the final parameter that
// declares the type.
func plusPlus(a, b, c int) int {
return plus(plus(a, b),c)
}
func main() {
// Call a function just as you'd expect, with
// `name(args)`.
res := plus(1, 2)
fmt.Println("1+2 =", res)
res = plusPlus(1, 2, 3)
fmt.Println("1+2+3 =", res)
}
- Complete the recursive implementation of plus so that the output matches the following. You may assume the input is positive.
$ go run functions.go
1+2 = 3
1+2+3 = 6
- Add a
minusfunction (recursively implemented in the same way, so that) the output looks like
$ go run functions.go
1+2 = 3
7-3 = 4
1+2+3 = 6
Pointers
- Time
- 25 minutes
- Activity
- Extend given example
- Save the following to
~/cs2613/labs/L19/pointers.go
// Go supports <em><a href="https://en.wikipedia.org/wiki/Pointer_(computer_programming)">pointers</a></em>,
// allowing you to pass references to values and records
// within your program.
package main
import "fmt"
// We'll show how pointers work in contrast to values with
// 2 functions: `zeroval` and `zeroptr`. `zeroval` has an
// `int` parameter, so arguments will be passed to it by
// value. `zeroval` will get a copy of `ival` distinct
// from the one in the calling function.
func zeroval(ival int) {
ival = 0
}
// `zeroptr` in contrast has an `*int` parameter, meaning
// that it takes an `int` pointer. The `*iptr` code in the
// function body then _dereferences_ the pointer from its
// memory address to the current value at that address.
// Assigning a value to a dereferenced pointer changes the
// value at the referenced address.
func zeroptr(iptr *int) {
*iptr = 0
}
func main() {
i := 1
fmt.Println("initial:", i)
zeroval(i)
fmt.Println("zeroval:", i)
// The `&i` syntax gives the memory address of `i`,
// i.e. a pointer to `i`.
zeroptr(&i)
fmt.Println("zeroptr:", i)
// Pointers can be printed too.
fmt.Println("pointer:", &i)
// A new pointer to a value can be created with the
// builtin function `new`.
p := new(42)
fmt.Println("value at *p:", *p)
zeroptr(p)
fmt.Println("value at *p:", *p)
}
- Add the following code to the end of
mainand defineswapint, so that the output of the program matches that below
*p=3
i=4
swapint(&i,p)
fmt.Println("after swap value at *p:", *p)
fmt.Println("after swap value in i", i)
$ go run pointers.go
initial: 1
zeroval: 1
zeroptr: 0
pointer: 0x344372b720f8
value at *p: 42
value at *p: 0
after swap value at *p: 4
after swap value in i: 3
Structs
- Time
- 20 minutes
- Activity
- Modify given example
- Save the following as "~/cs2613/labs/L19/intutils/structs.go"
// Go's _structs_ are typed collections of fields.
// They're useful for grouping data together to form
// records.
package main
import "fmt"
// This `person` struct type has `name` and `age` fields.
type person struct {
name string
age int
}
// `newPerson` constructs a new person struct with the given name.
func newPerson(name string) *person {
// Go is a garbage collected language; you can safely
// return a pointer to a local variable - it will only
// be cleaned up by the garbage collector when there
// are no active references to it.
p := person{name: name}
p.age = 42
return &p
}
func main() {
// This syntax creates a new struct.
fmt.Println(person{"Bob", 20})
// You can name the fields when initializing a struct.
fmt.Println(person{name: "Alice", age: 30})
// Omitted fields will be zero-valued.
fmt.Println(person{name: "Fred"})
// An `&` prefix yields a pointer to the struct.
fmt.Println(&person{name: "Ann", age: 40})
// It's idiomatic to encapsulate new struct creation in constructor functions
fmt.Println(newPerson("Jon"))
// Access struct fields with a dot.
s := person{name: "Sean", age: 50}
fmt.Println(s.name)
// Structs are mutable.
s.age = 51
fmt.Println(s)
// You can also use dots with struct pointers - the
// pointers are automatically dereferenced.
sp := &s
sp.age = 52
fmt.Println(sp.age)
// If a struct type is only used for a single value, we don't
// have to give it a name. The value can have an anonymous
// struct type. This technique is commonly used for
// [table-driven tests](testing-and-benchmarking).
dog := struct {
name string
isGood bool
}{
"Rex",
true,
}
fmt.Println(dog)
}