Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pointer to a Pointer in Go

Pointer to a Pointer in Go

Go PointersGo Pointers

If a pointer variable stores the address of another pointer variable, it is called 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 pointer-to-pointer variables above are of integer type.

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

package main



import "fmt"



func main() {



   var a int

   var ptr *int

   var pptr **int



   a = 3000



   /* pointer ptr address */

   ptr = &a



   /* pointer ptr address */

   pptr = &ptr



   /* Get the value of pptr */

   fmt.Printf("变量 a = %d\n", a )

   fmt.Printf("指针变量 *ptr = %d\n", *ptr )

   fmt.Printf("指向指针的指针变量 **pptr = %d\n", **pptr)

}

The output of the above example is:

变量 a = 3000

指针变量 *ptr = 3000

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

Go PointersGo Pointers