when learning go pointers, we think that the pointer variable stores the value of the memory address, and the * operator can get the stored value of the memory
.then when you join the pointer to the array ( array pointer ), you find that you can manipulate the array directly with the pointer instead of *
ordinary variable pointer operation
package main
impotr "fmt"
func main(){
pArr := [3]int{12,2,9}
pArr[0] = 16
pAdd := &pArr
(*pAdd)[0] = 444
fmt.Printf("pArr[0] %v\n",pArr[0])
fmt.Printf("pAdd[0] %v\n",pAdd[0])
fmt.Printf("pAdd %v\n",pAdd)
fmt.Printf("pArr %v\n",pArr)
fmt.Printf("pAdd %v\n",*pAdd)
fmt.Printf("pArr[0] %v\n",&pArr[0])
fmt.Printf("pAdd[0] %v\n",&pAdd[0])
fmt.Printf("(*pAdd)[0] %v\n",&(*pAdd)[0])
/*output
pArr[0] 444
pAdd[0] 444
pAdd &[444 2 9]
pArr [444 2 9]
pAdd [444 2 9]
pArr[0] 0xc04200c2e0
pAdd[0] 0xc04200c2e0
(*pAdd)[0] 0xc04200c2e0
*/
}
pay attention to the above,
directly manipulate the array pArr [0] = 16
I can understand
find the array through the pointer and then manipulate the array (* pAdd) [0] = 16
I can also understand
but ~!
what is the operation that can operate to the original array directly through the pointer? pAdd [0] = 16
this I don"t quite understand.
in this way, the operation is literally a pointer, but the original array is also changed. After output, it is found that everyone points to the same address value
pArr [0] 0xc04200c2e0
address of pAdd [0] 0xc04200c2e0
(* pAdd) [0] address 0xc04200c2e0
ps: subject does not have the foundation of c, which is a pointer language. I hope you can understand
. Finally, I would like to ask, how to understand the situation of manipulating arrays directly with pointers? < / H1 >