Open In App

Go Pointer to Pointer (Double Pointer)

Last Updated : 30 Sep, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Pointers in Go

Pointers in Go programming language or Golang is a variable which is used to store the memory address of another variable. A pointer is a special variable so it can point to a variable of any type even to a pointer. Basically, this looks like a chain of pointers. When we define a pointer to pointer then the first pointer is used to store the address of the second pointer. This concept is sometimes termed as Double Pointers.

How to Declare a pointer to pointer in Golang?
Declaring Pointer to Pointer is similar to declaring pointer in Go. The difference is we have to place an additional ‘*’ before the name of pointer name. This is generally done when we are declaring the pointer variable using the var keyword along with the type. Below example and image will explain the concept in much better way.

Example 1: In the below program the pointer pt2 stores the address of pt1 pointer. Dereferencing pt2 i.e. *pt2 will give the address of variable v or you can also say the value of pointer pt1. If you will try **pt2 then this will give the value of the variable v i.e. 100.

Double Pointer in Go




// Go program to illustrate the
// concept of the Pointer to Pointer
package main
   
import "fmt"
   
// Main Function
func main() {
   
        // taking a variable
        // of integer type
    var V int = 100
       
    // taking a pointer 
    // of integer type 
    var pt1 *int = &V
       
    // taking pointer to 
    // pointer to pt1
    // storing the address 
    // of pt1 into pt2
    var pt2 **int = &pt1
   
    fmt.Println("The Value of Variable V is = ", V)
    fmt.Println("Address of variable V is = ", &V)
   
    fmt.Println("The Value of pt1 is = ", pt1)
    fmt.Println("Address of pt1 is = ", &pt1)
   
    fmt.Println("The value of pt2 is = ", pt2)
   
    // Dereferencing the 
    // pointer to pointer
    fmt.Println("Value at the address of pt2 is or *pt2 = ", *pt2)
       
    // double pointer will give the value of variable V
    fmt.Println("*(Value at the address of pt2 is) or **pt2 = ", **pt2)
}


Output:

The Value of Variable V is =  100
Address of variable V is =  0x414020
The Value of pt1 is =  0x414020
Address of pt1 is =  0x40c128
The value of pt2 is =  0x40c128
Value at the address of pt2 is or *pt2 =  0x414020
*(Value at the address of pt2 is) or **pt2 =  100

Example 2: Let’s make some changes in the above program. Assigning some new value to the pointers by changing the value of pointers using dereferencing as shown below:

How Pointers Works In Go




// Go program to illustrate the
// concept of the Pointer to Pointer
package main
  
import "fmt"
  
// Main Function
func main() {
  
    // taking a variable
    // of integer type
    var v int = 100
  
    // taking a pointer
    // of integer type
    var pt1 *int = &v
  
    // taking pointer to
    // pointer to pt1
    // storing the address
    // of pt1 into pt2
    var pt2 **int = &pt1
  
    fmt.Println("The Value of Variable v is = ", v)
  
    // changing the value of v by assigning
    // the new value to the pointer pt1
    *pt1 = 200
  
    fmt.Println("Value stored in v after changing pt1 = ", v)
  
    // changing the value of v by assigning
    // the new value to the pointer pt2
    **pt2 = 300
  
    fmt.Println("Value stored in v after changing pt2 = ", v)
}


Output:

The Value of Variable v is =  100
Value stored in v after changing pt1 =  200
Value stored in v after changing pt2 =  300


Previous Article
Next Article

Similar Reads

How to print string with double quotes in Golang?
Whenever the user wants to double-quote a string, he can't simply write the string within double quotes inside the fmt.Printf() command. This prints only the text written inside those quotes. To print the string along with quotes, he can use various methods including certain escape characters. There are different ways in Golang to print a string wi
2 min read
Returning Pointer from a Function in Go
Prerequisite: Pointers in Go and Passing Pointers to Function Pointers in Go programming language or Golang is a variable which is used to store the memory address of another variable. We can pass pointers to the function as well as return pointer from a function in Golang. In C/C++, it is not recommended to return the address of a local variable o
3 min read
Golang Pointer to an Array as Function Argument
Prerequisite: Pointers in Golang Pointers in Go programming language or Golang is a variable which is used to store the memory address of another variable. Whereas an array is a fixed-length sequence which is used to store homogeneous elements in the memory. You can use the pointers to an array and pass that one as an argument to the function. To u
3 min read
Pointer to a Struct in Golang
Pointers in Golang is a variable which is used to store the memory address of another variable. Pointers in Golang is also termed as the special variables. The variables are used to store some data at a particular memory address in the system. You can also use a pointer to a struct. A struct in Golang is a user-defined type which allows to group/co
3 min read
How to find the capacity of the pointer in Golang?
Pointers in Go programming language or Golang is a variable which is used to store the memory address of another variable. Pointers in Golang are also termed as the special variables. The variables are used to store some data at a particular memory address in the system. The memory address is always found in hexadecimal format(starting with 0x like
2 min read
How to find the length of the pointer in Golang?
Pointers in Go programming language or Golang is a variable which is used to store the memory address of another variable. Pointers in Golang are also termed as the special variables. The variables are used to store some data at a particular memory address in the system. The memory address is always found in hexadecimal format(starting with 0x like
2 min read
How to instantiate Struct Pointer Address Operator in Golang?
As pointers are the special variables that are used to store the memory address of another variable whereas, the struct is user-defined data type that consists of different types. A struct is mainly a holder for all other data types. By using a pointer to a struct we can easily manipulate/access the data assigned to a struct. To use pointer to a st
2 min read
How to find the capacity of Channel, Pointer and Slice in Golang?
Go language, capacity defines the maximum number of elements that a particular can hold. Here the task is to find the capacity of Channel, Pointer, and Slice in Golang, we can use the cap() function. Syntax: func cap(l Type) int Here, the type of l is a pointer. Let us discuss this concept with the help of the examples: Example 1: In this example,
2 min read
reflect.Pointer() Function in Golang with Examples
Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Pointer() Function in Golang is used to get the v's value as a uintptr. To access this function, one needs to imports the reflect package in the program. Syntax: f
2 min read
How to check pointer or interface is nil or not in Golang?
In Golang, nil check is frequently seen in GoLang code especially for error check. In most cases, nil check is straight forward, but in interface case, it's a bit different and special care needs to be taken. Here the task is to check pointer or interface is nil or not in Golang, you can check with the following: Example 1: In this example, the poi
3 min read