Pointer is a data type whose value is the memory address of another value.
Pointers allow a program to refer to data indirectly instead of copying the data itself. They are common in systems programming and in languages that expose memory management details, such as C Language and Golang.
For example, in Go:
package main
import "fmt"
func main() {
value := 42
pointer := &value
fmt.Println(value) // prints 42
fmt.Println(pointer) // prints pointer address
fmt.Println(*pointer) // prints value pointed at (42)
}Pointers are useful for:
- Avoiding unnecessary copies of large values
- Allowing a function to modify a value owned by its caller
- Representing optional values with
nil - Building linked data structures
Pointers also introduce risks. A pointer may be nil, may outlive the value it refers to in unsafe languages, or may make ownership and mutation harder to reason about.
Anki
id: pointer-definition deck: Computer Science::Data Types tags: data-types pointer
Q: What is a pointer? A: A pointer is a data type whose value is the memory address of another value.
id: go-address-operator deck: Computer Science::Data Types tags: data-types pointer golang
Q: In Go, what does &value do?
A: It returns the memory address of value.
id: go-dereference-operator deck: Computer Science::Data Types tags: data-types pointer golang
Q: In Go, what does *pointer do?
A: It dereferences the pointer to access the value stored at that address.