Home > Backend Development > C#.Net Tutorial > The difference between C++ references and pointers

The difference between C++ references and pointers

Guanhui
Release: 2020-05-29 14:32:30
Original
3183 people have browsed it

The difference between C++ references and pointers

C The difference between a reference and a pointer

1. A pointer has its own space, while a reference is just an alias;

2. The pointer can be initialized to NULL, but the reference must be initialized and must be a reference to an existing object;

3. The meanings of operators used by pointers and references are different.

Pointer

The special thing about pointers is that the value stored in the memory space corresponding to the pointer variable happens to be a certain memory address. This is also one of the characteristics that distinguishes pointer variables from other variables. For example, a pointer is defined as follows:

int x = 5;
int *ptr = &x;
Copy after login

ptr is a correction variable name. Obtaining the value in the memory pointed to by this pointer through a pointer is called dereferencing. Null pointers cannot be dereferenced.

The representation of pointer memory space is as follows:

The difference between C++ references and pointers

##Quote

References are also often used in C, especially when used as function parameters and when the value outside the function needs to be modified and updated inside the function. First of all, we must make it clear that a reference is a special kind of pointer.

A reference is a constant pointer pointing to another object, which holds the storage address of the pointed object. And it will be automatically dereferenced when used, without the need to explicitly retrieve it like using a pointer.

For example, the definition of the quote is as follows:

int x = 5;
int &y = x;
Copy after login


Recommended tutorial: "
C#"

The above is the detailed content of The difference between C++ references and pointers. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
c++
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template