1 package main
2
3 import "fmt"
4
5 func main() {
6 /*
7 数组指针:首先是一个指针,一个数组的地址。
8 *[4]Type
9
10 指针数组:首先是一个数组,存储的数据类型是指针
11 [4]*Type
12
13
14 *[5]float64,指针,一个存储了5个浮点类型数据的数组的指针
15 *[3]string,指针,数组的指针,存储了3个字符串
16 [3]*string,数组,存储了3个字符串的指针地址的数组
17 [5]*float64,数组,存储了5个浮点数据的地址的数组
18 *[5]*float64,指针,一个数组的指针,存储了5个float类型的数据的指针地址的数组的指针
19 *[3]*string,指针,存储了3个字符串的指针地址的数组的指针
20 **[4]string,指针,存储了4个字符串数据的数组的指针的指针
21 **[4]*string,指针,存储了4个字符串的指针地址的数组,的指针的指针
22 */
23
24 // 1.创建一个普通的数组
25 arr1 := [4]int{1, 2, 3, 4}
26 fmt.Println(arr1)
27
28 // 2.创建一个指针,存储该数组的地址--->数组指针
29 var p1 *[4]int
30 p1 = &arr1
31 fmt.Println(p1) // &[1 2 3 4]
32 fmt.Printf("%p\n", p1) // 数组arr1的地址 0xc0000101e0
33 fmt.Printf("%p\n", &p1) // p1指针自己的地址 0xc000006030
34
35 // 3.根据数组指针,操作数组
36 (*p1)[0] = 100
37 fmt.Println(arr1) // [100 2 3 4]
38
39 p1[0] = 200 // 简化写法
40 fmt.Println(arr1) // [200 2 3 4]
41
42 // 4.指针数组
43 a := 1
44 b := 2
45 c := 3
46 d := 4
47 arr2 := [4]int{a, b, c, d}
48 arr3 := [4]*int{&a, &b, &c, &d}
49 fmt.Println(arr2) // [1 2 3 4]
50 fmt.Println(arr3) // [0xc000014100 0xc000014108 0xc000014110 0xc000014118]
51 arr2[0] = 100
52 fmt.Println(arr2) // [100 2 3 4]
53 fmt.Println(a) // 1 并没有改变a的值
54 *arr3[0] = 200
55 fmt.Println(arr3) // [0xc000014100 0xc000014108 0xc000014110 0xc000014118]
56 fmt.Println(a) // 200 改变的a的值
57
58 b = 1000
59 fmt.Println(arr2) // [100 2 3 4]
60 fmt.Println(arr3) // [0xc000014100 0xc000014108 0xc000014110 0xc000014118]
61 for i := 0; i < len(arr3); i++ {
62 fmt.Println(*arr3[i]) // 200 1000 3 4
63 }
64 }