Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's the difference between C++ pointers and references?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail the difference between C++ pointers and citations. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1. The difference between reference and pointer

The principles of pointers and references are very similar, so they are often compared in many cases, especially in interviews.

This article will sort out some similarities and differences between references and pointers.

1.1 similarities

Both are about the concept of address.

The pointer itself is a variable, the value it stores is a memory address, and the reference is an alias for a memory. We can use pointers or references to modify the value of the corresponding memory.

1.2 differences

References must be initialized at declaration time, and pointers can be used without

We cannot declare a variable reference and assign a value to it, we can only initialize it at the same time as the declaration:

Int a = 3 int & b; / illegal int & c = a; / / legal

The pointer does not have this restriction:

Int * p; / / legal

References can only be initialized once when declared, and then cannot point to other values, and pointers can

Once the reference declaration cannot be changed, the pointer can. To some extent, references are similar to constant pointers.

Int a = 3 int & b = an int const * p = & a

References must point to valid variables, and pointers can be empty

This is a huge difference between the two, and we can safely use it when we get a reference, because it must not be empty. On the other hand, the pointer is not. It may be null and must be judged before it can be used.

Sizeof operation results are different.

The sizeof function can calculate the size of the variable memory block, but if we use sizeof on the pointer, we get the memory occupied by the pointer itself, not the memory size of the variable the pointer points to. Citation does not have this problem.

There is a reference to the pointer, but no pointer to the reference

Let's first look at the pointer to the reference:

Int a = 3 int & b = an int * p = & b

This code does not report an error, but if we do run it, we will find that p is a normal int pointer that points to the variable a. Because b is a reference, its address is the same as a. So when we define a pointer to b, we actually define a pointer to a. This is why the pointer to the reference does not exist.

Let's take a look at the reference to the pointer. The reference to the pointer exists and is easy to understand, which is essentially an alias for the pointer:

Int a = 3 _ int * p = & a _ _ int * & pt = p

Pt can also point to other variables or modify the dereferenced value, which is no different from p in use.

In addition to the above, pointers and references have some minor differences. For example, the meaning of self-increment and self-subtraction is different. The self-increment and self-subtraction of the pointer represent the movement of the pointer, while the self-increment and self-subtraction of the reference is a change in the value of the variable.

This is the end of the article on "what's the difference between C++ pointers and quotes". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report