YouTip LogoYouTip

Go Array Of Pointers

# Go Array of Pointers [![Image 3: Go Pointers](#)Go Pointers](#) Before we understand pointer arrays, let's look at an example that defines an integer array of length 3: ## Example ```go package main import "fmt" const MAX int = 3 func main() { a := []int{10, 100, 200} var i int for i = 0; i < MAX; i++ { fmt.Printf("a[%d] = %dn", i, a) } } The output of the above code execution is: a = 10 a = 100 a = 200 There is a situation where we might need to store an array, so we need to use pointers. The following declares an integer pointer array: ```go var ptr *int; `ptr` is an integer pointer array. Therefore, each element points to a value. The following example stores three integers in a pointer array: ## Example ```go package main import "fmt" const MAX int = 3 func main() { a := []int{10, 100, 200} var i int var ptr *int; for i = 0; i < MAX; i++ { ptr = &a /* Assign address of integer to pointer array */ } for i = 0; i < MAX; i++ { fmt.Printf("a[%d] = %dn", i, *ptr) } } The output of the above code execution is: a = 10 a = 100 a = 200 [![Image 4: Go Pointers](#)Go Pointers](#)
← Go StructuresGo Pointer To Pointer β†’