The difference analysis of reference & and address & in C + + _c language

Source: Internet
Author: User

C + + in the reference & and Access & is a lot of beginners often error-prone places, today in this article on the analysis and summary for your reference.

In particular, one is used to pass the value of the first address is used to get the

& (Reference) ==> appears in a variable declaration statement at the left of the variable, indicating that the reference is declared.

For example:

int &rf; Declares a reference RF of type int

The & (take address operator) ==> the address of an object when it is assigned an initial value to an equal sign or when it appears as a unary operator in the execution statement.

In C + +, both the reference and the address, many people to the reference and address is not very clear, and therefore can not distinguish. In fact, their differences can be summed up in one sentence: and type together is a reference, and variables together is the access. Let's take a look at the concrete examples below

1 refers to the left of the assignment =, and the address is to the right of the assignment, for example:

int a=3 int
&b=a;    /reference
int *p=&a;    Take address

2 and type together is a reference, and variables together is the access. For example, as well as the following example:

int function (int &i)
{
 
}//reference

3 for vectors, the above 2 is equally suitable

Vector<int> VEC1 (10,1);  Initialize vec1:10 elements, every element ' s value is 1
vector<int> &vec2 = vec1;//VEC2 are reference to VEC1
vector<int> *vec3 = &vec2;//vec3 is addresss of VEC1 and VEC2

I hope this article will help you with the learning of C + + programming.

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.