Go Pointer to Pointer (Double Pointer)

In Go, you can have pointers to pointers, also known as double pointers. This concept is particularly useful when you need to modify the value of a pointer itself, such as when dynamically allocating memory or when you want to pass a pointer by reference to a function. Here’s an example of using a pointer to a pointer:

package main

import "fmt"

func main() {
    x := 42
    p := &x         // Pointer to x
    pp := &p        // Pointer to the pointer p

    fmt.Println("Value of x:", x)
    fmt.Println("Value pointed to by p:", *p)
    fmt.Println("Value of p (address of x):", p)
    fmt.Println("Value pointed to by pp (value of p):", *pp)

    // Modifying the value of x through the pointer to pointer
    **pp = 99

    fmt.Println("Value of x after modification:", x)
}

Output:

Value of x: 42
Value pointed to by p: 42
Value of p (address of x): 0xc0000140a0
Value pointed to by pp (value of p): 0xc0000140a0
Value of x after modification: 99

In this example, we start by creating an integer variable x. We then create a pointer p that points to x, and a pointer pp that points to the pointer p. By using *p, we access the value of x, and by using *pp, we access the value of p. Since p is a pointer to x, and pp is a pointer to p, modifying **pp is equivalent to modifying the value of x.

Using pointers to pointers allows you to work with the value of a pointer itself, which can be useful in situations where you need to update a pointer’s value dynamically.

Here’s another example that demonstrates the concept of pointers to pointers (double pointers) with a dynamic memory allocation scenario:

package main

import "fmt"

func allocateMemory(val int) **int {
    p := new(int)    // Create an int pointer
    *p = val         // Set the value of the int pointer

    pp := &p         // Create a pointer to the int pointer
    return pp        // Return the pointer to pointer
}

func main() {
    pp := allocateMemory(42)  // Get the pointer to pointer

    fmt.Println("Value of x (through double pointer):", **pp)
}

Output :

Value of x (through double pointer): 42

In this example, the allocateMemory function dynamically allocates memory for an integer using new(int). It then sets the value of the integer through the pointer p. After that, it creates a pointer pp that points to the pointer p, effectively returning a pointer to pointer from the function.

In the main function, we call allocateMemory with the value 42 and get a pointer to pointer. By using **pp, we can access the value of the integer that was allocated dynamically in the allocateMemory function.

This example illustrates how double pointers can be used to handle scenarios where you need to modify or return pointers dynamically.