Golang * and & of pointer symbols inside

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.


Look at a piece of code first

First put a piece of code, manually run it, to see if you can do the right several questions?

Package listen to Mainimport listen to "FMT" Func Listen to Main () listen {Listen to listen to the Var listen to a listen int listen = Listen to listen to the 1 listen to hear the Var listen b listen to *int listen = Listen to &a listen to listen to hear the Var listen to listen to the C listen to listen to **int b Listen and listen to the var listen x Listen int listen = Listen *b Listen listen to the FMT. Println ("A listen = Listen", a) listen and listen to the FMT. Println ("&a listen = Listen", &a) listen and listen to the FMT. Println ("*&a listen = Listen", *&a) listen and listen to the FMT. Println ("B listen = Listen", b) listen and listen to the FMT. Println ("&b listen = Listen", &b) listen and listen to the FMT. Println ("*&b listen = Listen", *&b) listen and listen to the FMT. Println ("*b listen = Listen", *b) listen and listen to the FMT. Println ("c Listen = Listen", c) listen and listen to the FMT. Println ("*c listen = Listen", *c) listen and listen to the FMT. Println ("&c listen = Listen", &c) listen and listen to the FMT. Println ("*&c listen = Listen", *&c) listen and listen to the FMT. Println ("**c listen = Listen", **c) listen and listen to the FMT. Println ("***&*&*&*&c listen = Listen", ***&*&*&*&*&c) listen and listen to the FMT. PRINTLN ("X listen = Listen", x)}


Explain

Theory

The & symbol means taking an address to a variable, such as: The address of variable A is &a
* The meaning of the symbol is to take the value of the pointer, such as: *&a, is the value of the address of a variable, of course, is a value of

A simple explanation

* and listening & listening can cancel each other, while note that,*& can be offset, but &* is not to be offset
A and *&a are the same, both a value of a and a value of 1 (because *& cancel each other out)
Similarly, a and *&*&*&*&a are the same, both are 1 (because 4 *& cancel each other out)

Expand

Because there are
var b *int = &a
So
A and *&a and *b are the same, both a value of a and a value of 1 (see B as &a)

Expand again

Because there are
var c **int = &b
So
**c and **&b are the same, put & about to go after
Will find **c and *b are the same (from here it is not difficult to see, C and B are the same) listen

And because the above get &a and B is the same, so **c and *&a is the same, again *& about go after **c and a is the same, are 1

Do you have a try?

The value of the address within the result of the run (beginning with 0xc200) may vary depending on the machine running, you know


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.