forked from FAuditore/golang-basic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointer.go
48 lines (37 loc) · 1.1 KB
/
pointer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import "fmt"
func main() {
/*
数组指针 *[n]type 一个指针,存储数组的地址
指针数组 [n]*type 一个数组,存储的数据类型是指针
函数指针 一个指向函数的指针
slice,map,function
指针函数 一个函数,返回值是一个指针
指针之间==,只有当它们指向同一个变量或全部是nil时才相等。
不可寻址: 即不可调用*T方法
字符串中的字节;
map 对象中的元素(slice 对象中的元素是可寻址的,slice的底层是数组);
常量;
包级别的函数等
*/
a := 10
fmt.Printf("a的地址: %p\n", &a)
var p1 *int
p1 = &a // p, of type *int, points to a
fmt.Println(p1) //a的地址
fmt.Printf("p1的地址: %p\n", &p1)
fmt.Println(*p1)
*p1 = 200
fmt.Println(a) //200
var p2 **int
p2 = &p1
fmt.Printf("%T, %T, %T\n", a, p1, p2)
fmt.Println(p2) //p1的地址
//数组指针
arr1 := [4]int{1, 2, 3, 4}
p3 := &arr1
fmt.Println(p3) //&[1,2,3,4]
fmt.Printf("%T %p\n", p3, p3) //*[4]int 数组的地址
p3[0] = 200 // (*p3)[0]=200
fmt.Println(arr1)
}