Skip to content

Commit bc543a7

Browse files
Apply suggestions from code review
Co-authored-by: June <[email protected]>
1 parent 25a35a4 commit bc543a7

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

concepts/pointers/about.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ The piece of memory that is associated with `a` will now will now be storing the
3131

3232
## Pointers
3333

34-
While variables allow us to refer to values in memory, sometimes it's useful to know the **memory address** to which the variable is pointing. **Pointers** hold the memory addresses of those values. A pointer declaration looks like this:
34+
While variables allow us to refer to values in memory, sometimes it's useful to know the **memory address** to which the variable is pointing. **Pointers** hold the memory addresses of those values. You declare a variable with a pointer type by prefixing the underlying type with an asterisk:
3535

3636
```go
3737
var p *int // 'p' contains the memory address of an integer
3838
```
3939

40-
Here we declaring a variable `p` of type `*int`. The type `*int` means "pointer to int". In other words, we are saying that `p` is a pointer to int, which means that `p` will hold the memory address of an integer. The zero value of pointers is `nil` because a `nil` pointer holds no memory address.
40+
Here we declaring a variable `p` of type "pointer to int" (`*int`). This means that `p` will hold the memory address of an integer. The zero value of pointers is `nil` because a `nil` pointer holds no memory address.
4141

4242
### Getting a pointer to a variable
4343

4444
To find the memory address of the value of a variable, we can use the `&` operator.
45-
For example, if we want to find and store the member address of variable `a` in the pointer `p`, we can do the following:
45+
For example, if we want to find and store the memory address of variable `a` in the pointer `p`, we can do the following:
4646

4747
```go
4848
var a int
@@ -54,7 +54,7 @@ p = &a // the variable 'p' will hold the memory address of 'a'
5454

5555
### Accessing the value via a pointer (dereferencing)
5656

57-
When we have a pointer, we might want to know the value stored in the memory address to which it points. We can do this using the `*` operator:
57+
When we have a pointer, we might want to know the value stored in the memory address the pointer represents. We can do this using the `*` operator:
5858

5959
```go
6060
var a int
@@ -87,6 +87,11 @@ Assigning to `*pa` will change the value stored at the memory address `pa` holds
8787

8888
A note of caution however: always check if a pointer is not `nil` before dereferencing. Dereferencing a `nil` pointer will make the program crash at runtime!
8989

90+
```go
91+
var p *int // p is nil initially
92+
fmt.Println(*p)
93+
// panic: runtime error: invalid memory address or nil pointer dereference
94+
9095
### Pointers to structs
9196

9297
So far we've only seen pointers to primitive values. We can also create pointers for structs:
@@ -143,9 +148,10 @@ fmt.Println(ages)
143148
// The changes the function 'addPeterAge' made to the map are visible after the function ends!
144149
```
145150
151+
The same applies when changing an existing item in a slice. However, actions that return a new slice like `append` will **not** modify the slice outside of the function.
146152
## Pointer arithmetic
147153
148-
Unlike other languages, pointer arithmetic is not allowed in Go. This means that snippets like the following are invalid:
154+
Unlike other languages, Go does not support pointer arithmetic. This means you cannot go from a memory address of one value to the address of another by incrementing the pointer value. So snippets like the following are invalid:
149155
150156
```go
151157
a := 2

concepts/pointers/introduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# About
1+
# Introduction
22

33
Like many other languages, Go has pointers.
44
If you're new to pointers, they can feel a little mysterious but once you get used to them, they're quite straight-forward.

exercises/concept/election-day/.docs/instructions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ elections are run to elect a new president.
66

77
In this year's election, it was decided that a new digital system to
88
count the votes was needed. The school needs your help building this new system.
9+
910
## 1. Create a vote counter
1011

1112
One of the first things that the new voting system needs is a vote counter.

exercises/concept/election-day/.meta/design.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The goal of this exercise is to teach the basics of pointers and how they work i
1515
## Out of scope
1616

1717
- Unsafe pointers
18+
- Pointer receivers for methods (this is parts of the `methods` concept)
1819

1920
## Concepts
2021

0 commit comments

Comments
 (0)