douxu3315 2017-06-19 23:56
浏览 42
已采纳

为什么数组的&(地址)在行中打印“&”

Here is the go code

package main 

func main() {

    var ax [2]int
    ax[0] = 22
    ax[1] = 99

    bx := ax
    cx := &ax

    fmt.Println(ax)
    fmt.Println(bx)
    fmt.Println(cx)
    fmt.Printf("%p
", cx)

}

When I execute it, it gives me the following output

PS C:\personal\gospace> ./bin/test
[22 99]
[22 99]
&[22 99]
0xc0420381d0

cx := &ax rightly interpreted cx as pointer. But when I print cx it prints &[22 99] and when I print &ax[0] or %p formatter for cx it rightly prints the address. Why is this behavior?

  • 写回答

1条回答 默认 最新

  • dongyaoxiu6244 2017-06-20 00:20
    关注

    Default printing verb fmt.Println uses is %v. While printing it differentiates value vs pointer value, that's why you see & in front of cx.

    fmt.Println(cx)
    

    Next, you specifically tell fmt.Printf to use the verb %p, refer to printing section and it prints base 16 notation, with leading 0x.

    fmt.Printf("%p
    ", cx)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿
  • ¥15 Matlab在app上输入带有矩阵形式的初始条件发生错误
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址