gpt4 book ai didi

go - 如何获取指针的地址?

转载 作者:数据小太阳 更新时间:2023-10-29 03:34:12 25 4
gpt4 key购买 nike

package main

import "fmt"

func main() {
var a int= 20
var ip *int

ip = &a

fmt.Printf("a address: %x\n", &a )

fmt.Printf(" the adrress that ip stored: %x\n", ip )
/*I try to get the address of variable ip */
fmt.Printf(" the address of ip: %d\n", &ip )

}

go run a.go

结果:

地址:c420016078ip存储的地址:c420016078 ip地址:842350510120

我的问题是:842350510120是正确的ip地址吗?

最佳答案

如果您以一致的基数(十六进制)打印地址,您将看到预期的结果。在当前版本的 Go gc 编译器中,变量 ipi 分配在堆上。

例如,

package main

import "fmt"

func main() {
var i int = 20
var pi *int = &i
fmt.Printf("%x the address of i\n", &i)
fmt.Printf("%x the address of pi\n", &pi)
fmt.Printf("%x the address that pi stored\n", pi)
}

输出:

c00008e010 the address of i
c000090018 the address of pi
c00008e010 the address that pi stored

关于go - 如何获取指针的地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53481035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com