References and pointers in C/C ++

Source: Internet
Author: User

 

A reference is also called an alias. It can be the most object name, but it is neither an object pointer nor a copy of an object.

 

Objects can be indirectly operated through reference. The usage is similar to a pointer, but the pointer syntax is not required.

 

 

 

Rules:

 

1> when a reference is created, it must be initialized. (The pointer can be initialized at any time.

 

Int a = 12;

 

Int & B = a; // correct

 

Int & c; // Error

 

2> once the reference is initialized, the reference relationship cannot be changed (the pointer can change the object at any time)

 

3> all referenced operations are actually applied to the objects it refers to, including the addressing operators.

 

4> a null reference is not allowed. The reference must be associated with a valid storage unit (the pointer can be NULL)

 

 

 

Direct Access: Access variable values by variable address

 

Indirect access: access the variable through the variable of the variable address

 

 

 

Pointer: the pointer is the variable whose value represents the memory address.

 

If both pointers point to elements in the same array, they can be subtracted.

 

Any pointer can be compared to test whether they are equal or not.

 

 

 

Similarities and differences between pointers and references

 

 

 

Similarities:

 

It is the concept of address. The Pointer Points to a piece of memory. Its content refers to the address of memory, and the reference is the alias of a piece of memory.

 

 

 

Differences:

 

1> A pointer is an entity, and a reference is an alias.

 

2> the reference can only be initialized once during definition, and cannot be changed later. The pointer can. The reference is "from one end to the end", and the pointer is "thinking differently".

 

3> the reference has no const, the pointer has const, And the pointer with const cannot be changed.

 

4> the reference cannot be empty, but the pointer can be empty.

 

5> Sizeof references the size of the variable (object), while sizeof obtains the size of the pointer.

 

6> pointer and reference auto-increment (++) operations have different meanings

 

7> references are type-safe, but pointers are not (references have more type checks than pointers)

 

From: Feige's column

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.