Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set an int pointer to an int value in Go

Tags:

pointers

go

I'm very new to Go and am trying to set a *int to point to a number, say 12345.

package main

import (
    "fmt"
    "reflect"
)

func main() {
    var guess *int
    fmt.Println(reflect.TypeOf(guess))
    *guess = 12345
    fmt.Println(guess)
}

But it's giving me the following error:

Type: *int
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x483c7d]

goroutine 1 [running]:
main.main()
    /home/aaron/projects/gopath/src/github.com/AaronNBrock/go-guess/main.go:16 +0x9d
exit status 2

I see that the error is with the *guess = 12345 since 12345, but I'm not sure what's causing it.

like image 379
Aaron N. Brock Avatar asked Jun 13 '18 06:06

Aaron N. Brock


People also ask

Can you assign a pointer to an int?

No, it is no valid to assign a pointer to an integer. It's a constraint violation of the assignment operator (C99, 6.5. 16.1p1).

Can pointer store integer value?

Note: The pointer is absolutely never used to access a memory location, just to store an int.

Can you assign a pointer to a variable?

You need to initialize a pointer by assigning it a valid address. This is normally done via the address-of operator (&). The address-of operator (&) operates on a variable, and returns the address of the variable. For example, if number is an int variable, &number returns the address of the variable number.

How do you represent a pointer in Go?

In Go a pointer is represented using the * (asterisk) character followed by the type of the stored value. In the zero function xPtr is a pointer to an int . * is also used to “dereference” pointer variables. Dereferencing a pointer gives us access to the value the pointer points to.


1 Answers

You have a pointer variable which after declaration will be nil.

If you want to set the pointed value, it must point to something. Attempting to dereference a nil pointer is a runtime panic, just what you experienced. You may use the builtin new() function to obtain a pointer to a zero-valued int, and then you can set the pointed value:

var guess *int
guess = new(int)
*guess = 12345

Your modified app:

var guess *int
fmt.Println(guess)
guess = new(int)
*guess = 12345
fmt.Println(guess, *guess)

Output (try it on the Go Playground):

<nil>
0x10414028 12345

Note that you can make this shorter using a short variable declaration like this:

guess := new(int)
*guess = 12345

Another option to make a pointer point to something "useful" is to assign the address of a variable to the pointer variable, like this:

value := 12345 // will be of type int
guess := &value

But this solution modifies the pointer value, not the pointed value. The result is the same though in this simple example.

You could also just assign the address of another variable, and then proceed to change the pointed value:

var value int
guess := &value
*guess = 12345

Also note that since guess points to value, changing the pointed value will change the value of the value variable too. Also if you change the value variable directly, the pointed value by guess also changes: they are one and the same:

var value int
guess := &value
value = 12345
fmt.Println(*guess) // This will also print 12345

Try this one on the Go Playground.

like image 79
icza Avatar answered Sep 22 '22 19:09

icza