Go 数组指针(指向数组的指针)

demo.go(数组指针):

package main

import "fmt"

func main() {
	a := [5]int{1, 2, 3, 4, 5}
	p := &a   // p数组指针(指向数组的指针)
	fmt.Println(*p)  // [1 2 3 4 5]
	fmt.Println((*p)[0])  // 1    *p[0]的写法是错误的,[0]的优先级大于*
	fmt.Println(p[0])  // 1   (*p)[0]可以简写成p[0]  (如果是切片指针,则不能这样简写)

	fmt.Println(len(*p))  // 5
	fmt.Println(len(p))  // 5   len(p)和len(*p)相同,都表示指针指向的数组的长度。 (如果是切片指针,则只能使用len(*p))
	// 结构体的指针p  (*p).成员名 可以简写成 p.成员名
}

猜你喜欢

转载自blog.csdn.net/houyanhua1/article/details/88685553
今日推荐