p是一个指向数组的指针arr,我们可以arr通过 using获取数组*p,但是为什么不能通过 using 获取第二个元素*p[2]?
它会导致错误:
p[1] 的无效间接(int 类型)
以下代码:
arr := [4]int{1,2,3,4}
var p *[4]int = &arr
fmt.Println(p) // output &[1 2 3 4]
fmt.Println(*p) // output [1 2 3 4]
fmt.Println(p[1]) // output 2
fmt.Println(*p[1]) //generate an error:invalid indirect of p[1] (type int)
Run Code Online (Sandbox Code Playgroud)