'&' And '*' (c ++)

Source: Internet
Author: User
'&' And '*' (c ++)

Author: Prelude

It depends on what is meant &. since C ++ makes use of overloaded built in operators, & can either take the address of an object, perform a bitwise AND operation, do anything related to a user defined type if overloaded by that type, or create a reference. in the context of this question, I'll assume a reference is meant.

A reference is an alternative name for an object, a synonym that refers to the same location in memory, but has a different name. the biggest difference between a pointer and a reference is that a reference need not be dereferenced.

Example of a reference:

Int I = 1;
Int & R = I; // I and R refer to the same int

R = 2; // I = 2

A pointer on the other hand, is a separate variable that contains a memory address as its value, that memory address can be dereferenced, or followed, to reach the contents of that memory address. the functionality of pointers are very similar to that of references:

Int I = 1;
Int * P = & I; // P now points to the address of I

* P = 2 // I = 2;

The biggest difference between references and pointers is that pointers may be treated like any other variable, operations can be med on the pointer itself:

Char * P = "test ";
P ++; // P = "est ";

References cannot have operations saved med on them since they are not variables, they are synonyms for the referred object and any operations will act on that object, not the reference.

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.