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 understand this concept let’s take an example. In the below program we are taking a pointer array arr which has 5 elements. We want to update this array using a function. Means modifying the array(here the array is {78, 89, 45, 56, 14}) inside the function and that will reflect at the caller. So here we have taken function updatearray which has pointer to array as argument type. Using updatearray(&arr) line of code we are passing the address of the array. Inside function (*funarr)[4] = 750 or funarr[4] = 750 line of code is using dereferencing concept to assign new value to the array which will reflect in the original array. Finally, the program will give output [78 89 45 56 750].
// Golang program to pass a pointer to an // array as an argument to the function package main import "fmt" // taking a function func updatearray(funarr *[5]int) { // updating the array value // at specified index (*funarr)[4] = 750 // you can also write // the above line of code // funarr[4] = 750 } // Main Function func main() { // Taking an pointer to an array arr := [5]int{78, 89, 45, 56, 14} // passing pointer to an array // to function updatearray updatearray(&arr;) // array after updating fmt.Println(arr) } |
Output:
[78 89 45 56 750]
Note: In Golang it is not recommended to use Pointer to an Array as an Argument to Function as the code become difficult to read. Also, it is not considered a good way to achieve this concept. To achieve this you can use slice instead of passing pointers.
Example:
// Golang program to illustrate the // concept of passing a pointer to an // array as an argument to the function // using a slice package main import "fmt" // taking a function func updateslice(funarr []int) { // updating the value // at specified index funarr[4] = 750 } // Main Function func main() { // Taking an slice s := [5]int{78, 89, 45, 56, 14} // passing slice to the // function updateslice updateslice(s[:]) // displaying the result fmt.Println(s) } |
Output:
[78 89 45 56 750]
Recommended Posts:
- Go Pointer to Pointer (Double Pointer)
- Golang program that uses func with variable argument list
- reflect.Pointer() Function in Golang with Examples
- Function that takes an interface type as value and pointer in Golang
- Pointer to a Struct in Golang
- How to find the capacity of the pointer in Golang?
- How to find the length of the pointer in Golang?
- How to instantiate Struct Pointer Address Operator in Golang?
- How to find the capacity of Channel, Pointer and Slice in Golang?
- How to check pointer or interface is nil or not in Golang?
- How to declare and access pointer variable in Golang?
- How to find the Length of Channel, Pointer, Slice, String and Map in Golang?
- How to Copy Struct Type Using Value and Pointer Reference in Golang?
- Returning Pointer from a Function in Go
- How to pass an Array to a Function in Golang?
- How to Copy an Array into Another Array in Golang?
- Copy an Array by Value and Reference into Another Array in Golang
- math.Lgamma() Function in Golang with Examples
- math.Float64bits() Function in Golang With Examples
- atomic.AddInt64() Function in Golang With Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

