Go Pointer to Pointer (Double Pointer)


Pointers are a powerful feature in Go that allow you to manipulate and manage memory more efficiently. In Go, a pointer is a variable that stores the memory address of another variable. Pointers are used to pass values by reference and to allocate and deallocate memory dynamically. Go also supports a pointer to a pointer, which is also known as a double pointer. In this article, we'll explore what a double pointer is, how it works, and how to use it in Go.

What is a Double Pointer?

A double pointer, or a pointer to a pointer, is a pointer variable that stores the memory address of another pointer variable. In other words, it's a pointer that points to another pointer. Double pointers are used when you want to pass a pointer variable by reference to a function or when you want to allocate memory dynamically.

How Does a Double Pointer Work?

A double pointer works by storing the memory address of another pointer variable. When you create a double pointer, you create a pointer variable that points to another pointer variable. You can then use the double pointer to manipulate the memory location of the original pointer variable.

For example, suppose you have a pointer variable called p that points to an integer variable i. You can create a double pointer pp that points to the memory address of p. You can then use the double pointer to change the value of p, which in turn changes the value of i.

Here's an example of how to create and use a double pointer in Go −

Example

package main

import "fmt"

func main() {
   var i int
   var p *int
   var pp **int
   
   i = 42
   p = &i
   pp = &p
   
   fmt.Printf("Value of i: %d\n", i)
   fmt.Printf("Value of p: %d\n", *p)
   fmt.Printf("Value of pp: %d\n", **pp)
   
   **pp = 21
   
   fmt.Printf("Value of i: %d\n", i)
   fmt.Printf("Value of p: %d\n", *p)
   fmt.Printf("Value of pp: %d\n", **pp)
}

Output

Value of i: 42
Value of p: 42
Value of pp: 42
Value of i: 21
Value of p: 21
Value of pp: 21

In the above example, we first create an integer variable i and a pointer variable p that points to i. We then create a double pointer variable pp that points to the memory address of p.

We print the initial values of i, p, and pp using the fmt.Printf() function. We then change the value of i by changing the value of **pp. This changes the value of p and therefore i.

Finally, we print the updated values of i, p, and pp using the fmt.Printf() function.

When to Use a Double Pointer?

A double pointer is typically used when you want to pass a pointer variable by reference to a function or when you want to allocate memory dynamically. For example, if you want to allocate memory for a two-dimensional array dynamically, you can use a double pointer to allocate the memory.

Conclusion

In this article, we've explored what a double pointer is, how it works, and how to use it in Go. Double pointers are a powerful feature of Go that allow you to manipulate and manage memory more efficiently. By understanding how to use double pointers, you'll be able to write more efficient and effective Go code

Updated on: 18-Apr-2023

366 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements