The difference between pointers and reference operators _string

Source: Internet
Author: User

The difference between pointers and reference operators

Pointers and references are similar concepts, but they are not the same. They all point to an object, but the pointer can allow that point to change at any time, and it allows you to point to an empty object. A reference is not the same, the reference must initially point to an object, and this point can never change, that is, can always point to this object, can not be changed. As you can see, the pointer is the point of a variable, and the reference is the point of a constant.

Pointers and references are also different in operation, and pointers use operators * and->, referencing the use of Operators & and. (dots).

For example, when they are defined, they use different operators:

Define pointer: int *number; can be assigned without initial value

Definition Reference: string s = "Hangcool";

string& PS = s; The value must be initially assigned so that the PS points to S.

Use a different operator when you point to a member of a variable:

Pointer pointing: x->y; The member Y pointing to the X pointer;

Reference point: x.y; Points to the member y referenced by X;

Of course, we can also write this: (*x). Y is equivalent to x->y.

It must be clear in the concept of the pointer that x represents an address value that points to an address, and *x represents a variable value that points to an address.

In addition, they have an important purpose in addition to the use of * and & in defining pointers and references:

Using * and & before variables have different meanings:

*: called a reference operator. That represents the content to be taken by the address that the expression represents. It can be translated as "... The value to point to ("value pointed by").

For example, * Mypointer can be read as "Mypointer pointing value".

&: Called an address or dereference operator. It is used as a variable prefix that can be translated as "... "(" Address of "),

For example, &variable1 can be read as a variable1 address. From: Blog.csdn.net/acosoft


Related Article

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.