简体   繁体   English

当我:=&b时,“&i”是什么意思

[英]What's the meaning of '&i' when i := &b

I'm learning the go, I tried the '&' to get the memory address.But I werid that what's the meaning of '&i',and 'i' came from i := &b , b is a int. 我正在学习编程,我尝试了'&'来获取内存地址。但是我很想知道'&i'是什么意思,而'i'来自i:=&b,b是一个整数。

b := 7
i := &b
fmt.Println(&b) //print => 0xc000088000
fmt.Println(i) //print => 0xc000088000
fmt.Println(&i) //print => 0xc00000e018

In this case , What's the meaning of '&i'? 在这种情况下,“&i”是什么意思?

& is the address operator , evaluating it results in a memory address, which when passed to the fmt package, usually the memory address is printed in hexadecimal format ("base 16 notation, with leading 0x"). &地址运算符 ,评估得出一个内存地址,该内存地址在传递给fmt包时,通常以十六进制格式(“ base 16表示法,前导0x”)打印。

A memory address is just that: a memory address. 内存地址就是这样:一个内存地址。 It doesn't matter if it's an address of an int variable or a string , or a variable of a pointer type. 不管是int变量或string的地址,还是指针类型的变量的地址。 When printed, they all look the "same". 当打印时,它们看起来都“相同”。

The address operator: 地址运算符:

For an operand x of type T , the address operation &x generates a pointer of type *T to x . 一个操作数x型的T ,写入动作&x生成类型的指针*Tx

So the address operator gives you a pointer value which when you dereference, you get back the original value. 因此,地址运算符为您提供了一个指针值,当您取消引用时,该指针值将返回原始值。

&b will be an address of the variable b , of type *int , which when you dereference: *b will give you (the value of) b . &b将是可变的地址b ,类型*int ,其中,当取消引用: *b会给你(的值) b

&i will be the address of the variable i , of type **int , which when you dereference: *i will give you the value of i which is the address of b . &i**int类型的变量i的地址,当您取消引用时: *i将为您提供i的值, i b的地址。 So if you also dereference that: **(&i) , that will also give you (the value of) b . 因此,如果您还取消引用**(&i) ,那也将给您b的值。

Here &b returns address of b and the same address is stored in i . 这里&b返回的地址b和相同的地址被存储在i Since i is also a variable &i will return the address of variable i. 由于i也是变量,因此&i将返回变量i的地址。

So & operator generates a pointer to it's operand. 因此, &运算符生成一个指向其操作数的指针。 So &i basically generates a pointer to i which is already a pointer to b which is nothing but a memory address. 因此&i基本上会生成一个指向i的指针,该指针已经是一个指向b的指针,而b只是内存地址。 So when you do fmt.Println(&i) it prints the memory address of i . 所以,当你做fmt.Println(&i)它打印的内存地址i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM