p
is a pointer which points to array arr
, we can get array arr
by using *p
, but why can not get second element by using *p[2]
?
It will cause an error :
invalid indirect of p[1] (type int)
The following code:
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)