Go Language Pointer to Pointer
If a pointer variable stores the address of another pointer variable, it is referred to as a pointer to a pointer variable.
When defining a pointer to a pointer variable, the first pointer stores the address of the second pointer, and the second pointer stores the address of the variable:
The declaration format for a pointer to a pointer variable is as follows:
var ptr **int;
The above pointer to a pointer variable is of integer type.
To access the value of a pointer to a pointer variable, two asterisks (*) are required, as shown below:
package main
import "fmt"
func main() {
var a int
var ptr *int
var pptr **int
a = 3000
/* Address of pointer ptr */
ptr = &a
/* Address of pointer to pointer ptr */
pptr = &ptr
/* Get the value of pptr */
fmt.Printf("Variable a = %d\n", a )
fmt.Printf("Pointer variable *ptr = %d\n", *ptr )
fmt.Printf("Pointer to pointer variable **pptr = %d\n", **pptr)
}
The output of the above example is:
Variable a = 3000
Pointer variable *ptr = 3000
Pointer to pointer variable **pptr = 3000