Pointers to pointers in Go language

Go Pointer

If a pointer variable stores the address of another pointer variable, then this pointer variable is called a pointer-to-pointer variable.

When defining a pointer-to-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-pointer variable is as follows:

var ptr **int;

The above pointer-to-pointer variable is of integer type.

To access the value of a pointer-to-pointer variable, two asterisks are needed, as shown below:

package main

import "fmt"

func main() {

   var a int
   var ptr *int
   var pptr **int

   a = 3000

   /* 指针 ptr 地址 */
   ptr = &a

   /* 指向指针 ptr 地址 */
   pptr = &ptr

   /* 获取 pptr 的值 */
   fmt.Printf("变量 a = %d\n", a )
   fmt.Printf("指针变量 *ptr = %d\n", *ptr )
   fmt.Printf("指向指针的指针变量 **pptr = %d\n", **pptr)
}

The output result of the above example execution is:

变量 a = 3000
指针变量 *ptr = 3000
指向指针的指针变量 **pptr = 3000

Go Pointer

other extensions