Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In the Go programming language or Golang, pointers are variables that store the memory address of other variables. Since a pointer is a specific variable, it can point to any type of variable, even another pointer. This resembles a list of pointers. The first pointer is used to hold the address of the second pointer when we define a pointer to pointer. This concept is also known as a double pointer in Go programming.
What is Pointer to Pointer?
A chain of pointers is a kind of pointer to a pointer or double pointer in Go. A pointer typically contains a variable's address. The location of the real value is indicated by the address of the second pointer and is included in the first pointer when we define a pointer to a pointer, as shown below.
Declaration of Pointer to Pointer
Declaring a pointer in Go is comparable to declaring a pointer to a pointer or a double pointer in Go. The distinction is that we need to put an extra "*" before the pointer name. This is often done when we use the ‘var’ keyword and the type when declaring the pointer variable. The example and the picture below will help to illustrate the idea better of the double-pointer in Go.
Example
The address of the pointer ‘p1’ is stored in the pointer ‘p2’, as shown below in the program. The variable ‘v’ address will be revealed by dereferencing pointer p2 or *p2. You can say the value of pointer p1. You can get the variable a's value, which is 100, by trying **p2.
/*
Using the Go Programming language, demonstrate the Pointer to Pointer(Double Pointer in Go) principle.
*/
package main
import "fmt"
// Function main()
func main() {
// Taking an integer type variable
var a int = 300
// Take integer type pointer
var p1 *int = &a
// Transferring pointer to pointer to p1 and saving p1's address in p2
var p2 **int = &p1
fmt.Println("Variable a's value is = ", a)
fmt.Println("Variable a's address is = ", &a)
// Variable a's value will be provided using a double pointer.
fmt.Println("*(p2's address value is) or **p2 = ", **p2)
fmt.Println("p1's value is = ", p1)
fmt.Println("p1's address is = ", &p1)
fmt.Println("p2's value is = ", p2)
// Pointer to pointer dereferencing
fmt.Println("p2's address value is or *p2 = ", *p2)
}
Output
Let's alter the program mentioned above. Dereferencing is used to change the value of pointers and assign them a new value, as demonstrated below:
/*
Using the Go Programming language, demonstrate the Pointer to Pointer or double pointer in Go.
*/
package main
import "fmt"
// Function main()
func main() {
// Taking an integer type variable
var a int = 300
// Take integer type pointer
var p1 * int = &a
// Transferring pointer to pointer to p1 and saving p1's address in p2
var p2 ** int = & p1
fmt.Println("Variable a's value is = ", a)
// By giving the pointer p1 the new value, you can change the value of a.
* p1 = 450
fmt.Println("After altering p1, value is retained in a = ", a)
// By giving the pointer p2 the new value, you can change the value of a.
** p2 = 650
fmt.Println("After altering p2, value is retained in a = ", a)
}
Output
Frequently Asked Questions
Why is pointer to pointer necessary?
Variable addresses are stored via pointers. Therefore, the first pointer is utilized to store the address of the second pointer when we define a pointer to pointer.
What do you mean by a pointer, and why is it used?
Blocks of memory that are dynamically allocated are managed and stored using pointers.
Can you expand pointer to pointer?
You may go further with the pointer notion. As we've already seen, a pointer variable can be given an ordinary variable's address. This variable itself might be a different pointer.
In Golang, are pointers secure?
Yes, it is secure due to escape analysis performed by the Go compiler and heap allocation of such variables.
Using Go, how do I dereference a pointer?
Asterisks * should be placed before any pointers to dereference them.
Conclusion
This article discusses the Go programming language's Double pointer in Go. We have discussed its definition, its uses, and how we can implement it with the help of simple examples.