4

go语言指针符号的*和&

 2 years ago
source link: https://studygolang.com/articles/7412?fr=sidebar
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

先看一段代码

先放一段代码,人工运行一下,看看自己能做对几题?

package main

import "fmt"

func main() {
    var a int = 1 
    var b *int = &a
    var c **int = &b
    var x int = *b
    fmt.Println("a = ",a)
    fmt.Println("&a = ",&a)
    fmt.Println("*&a = ",*&a)
    fmt.Println("b = ",b)
    fmt.Println("&b = ",&b)
    fmt.Println("*&b = ",*&b)
    fmt.Println("*b = ",*b)
    fmt.Println("c = ",c)
    fmt.Println("*c = ",*c)
    fmt.Println("&c = ",&c)
    fmt.Println("*&c = ",*&c)
    fmt.Println("**c = ",**c)
    fmt.Println("***&*&*&*&c = ",***&*&*&*&*&c)
    fmt.Println("x = ",x)
}

&符号的意思是对变量取地址,如:变量a的地址是&a
*符号的意思是对指针取值,如:*&a,就是a变量所在地址的值,当然也就是a的值了

简单的解释

*和 & 可以互相抵消,同时注意,*&可以抵消掉,但&*是不可以抵消的
a*&a是一样的,都是a的值,值为1 (因为*&互相抵消掉了)
同理,a*&*&*&*&a是一样的,都是1 (因为4个*&互相抵消掉了)

因为有
var b *int = &a
所以
a*&a*b是一样的,都是a的值,值为1 (把b当做&a看)

因为有
var c **int = &b
所以
**c**&b是一样的,把&约去后
会发现**c和`
b是一样的 (从这里也不难看出,cb也是一样的) 又因为上面得到的&ab是一样的 所以**c&a是一样的,再次把*&约去后**ca`是一样的,都是1

不信你试试?

运行的结果内的地址值(0xc200开头的)可能会因不同机器运行而不同,你懂的

$ go run main.go 
a     =     1
&a     =     0xc200000018
*&a     =     1
b     =     0xc200000018
&b     =     0xc200000020
*&b     =     0xc200000018
*b     =     1
c     =     0xc200000020
*c     =     0xc200000018
&c     =     0xc200000028
*&c     =     0xc200000020
**c     =     1
***&*&*&*&c     =     1
x     =     1

两个符号抵消顺序

*&可以在任何时间抵消掉,但&*不可以被抵消的,因为顺序不对

fmt.Println("*&a\t=\t",*&a)  //成功抵消掉,打印出1,即a的值
fmt.Println("&*a\t=\t",&*a)  //无法抵消,会报错

有疑问加站长微信联系(非本文作者)

280

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK