funcmain0401(){var p *[]int
fmt.Printf("%p\n", p)
p =new([]int)
fmt.Printf("%p\n", p)*p =append(*p,1,2,3)//for i := 0; i<len(*p);i++ {// fmt.Println((*p)[i])//}for i, v :=range*p {
fmt.Println(i, v)}//fmt.Println(len(*p))}
5.指针数组
funcmain0501(){//var arr [3]int ->int//指针数组var arr [3]*int//->*int
a :=10
b :=20
c :=30
arr[0]=&a
arr[1]=&b
arr[2]=&c
//fmt.Println(arr)//fmt.Printf("%p\n",&a)//fmt.Printf("%p\n",&b)//fmt.Printf("%p\n",&c)//通过指针数组改变变量的值//*arr[1]=2////fmt.Println(b)//变量指针数组对应的内存空间的值for i :=0; i <len(arr); i++{
fmt.Println(*arr[i])}}
6.指针切片
funcmain0601(){//指针切片var slice []*int
a :=10
b :=20
c :=30
d :=40
slice =append(slice,&a,&b,&c)
slice =append(slice,&d)
fmt.Println(slice)for i, v :=range slice {//fmt.Printf("%T",v)
fmt.Println(i, v)
fmt.Println(i,*v)}}
7.结构体指针
type Student struct{
name string
id int
age int
sex string}funcmain0701(){// 定义结构体变量// var 结构体名 结构体数据类型var stu Student = Student{id:101, name:"多啦A梦", age:100, sex:"男"}// 结构体变量.成员=值// 定义结构体指针指向变量的地址var p *Student
//结构体指针指向结构体变量地址
p =&stu
p1 :=&stu
p2 :=&stu.name
fmt.Printf("%T\n", p1)//*Student
fmt.Printf("%T\n", p2)//*int
fmt.Printf("%T", p)
fmt.Printf("%p\n",&stu)
fmt.Printf("%p\n",&stu.id)
fmt.Printf("%p\n",&stu)//结构体成员地址
fmt.Printf("%p\n",&stu.name)
fmt.Printf("%p\n",&stu.id)
fmt.Printf("%p\n",&stu.age)
fmt.Printf("%p\n",&stu.sex)}
funcmain0702(){var stu Student = Student{id:101, name:"多啦A梦", age:100, sex:"男"}var p *Student
p =&stu
//通过结构体指针间接操作结构体成员//(*p).name="大熊"//通过指针可以直接操作结构体成员
p.name ="静香"
p.age =18
p.sex ="女"
fmt.Println(stu)}
funcmain0703(){//结构体切片var stu []Student =make([]Student,3)
p :=&stu //*[]Student //结构体切片指针//stu[0]=Student{"小猪佩奇",1,10,"女"}(*p)[0]= Student{"小猪佩奇",1,10,"女"}(*p)[1]= Student{"野猪佩奇",2,15,"女"}(*p)[2]= Student{"猪刚鬣",3,1000,"男"}//append在切片长度后添加数据*p =append(*p, Student{"小猪佩奇",1,10,"女"})//fmt.Printf("%T\n",p)//fmt.Println(stu)for i :=0; i <len(*p); i++{
fmt.Println((*p)[i])}}
8.多级指针
funcmain0801(){
a :=10
p :=&a
pp :=&p //二级指针 二级指针存储一级指针的地址
p3 :=&pp //三级指针 存储二级指针的地址//fmt.Println(p)//fmt.Println(&p)//一级指针的值//fmt.Println(*pp)//fmt.Println(p)//fmt.Println(&a)//变量a 的值
fmt.Println(*p3)
fmt.Println(**pp)
fmt.Println(*p)
fmt.Println(a)//fmt.Printf("%T\n",a)//fmt.Printf("%T\n",p)//fmt.Printf("%T\n",pp)//fmt.Printf("%T\n",ppp)}