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

How to use the variable of C++

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

Share

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

Today, the editor will share with you the relevant knowledge about how to use C++ variables. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.

Variable name: the nickname given by the programmer to the address

At school, the teacher said that variables are stored in memory, memory is like rows of drawers, each drawer has a number, we define a variable, that is, put what you want to put in this corresponding numbered drawer. For example: int a = 10, which is represented by the figure below:

Here: the name of the variable is a, the value of the variable is: 10, and the address of the variable is 0x 00000001.

So the problem is, we know that the value of the variable is in the drawer (in memory), each drawer has a number (address), but where is the name of the variable a stored? Or will it exist in memory?

Let's think of a question: if the name of a variable is to be stored in memory, then a space must be allocated to it, and there is an address in the space where it is stored. Does this address have to be stored somewhere before the program can find it? If it is really designed in this way, then the code is not written at all and cannot be run at all.

In fact, the variable name is only written for the programmer, let us know what the use of this variable is when we write the code, and we can call the value of the variable through the name. Because if I give you an address 0x 23004123 directly, do you know what this is about? After the code has been compiled, it will eventually be converted into machine code, and the variable names we define will no longer exist, only the address and value.

The pointer is actually very ordinary.

With the above understanding, let's have a special variable: the pointer variable. What is a pointer variable? In fact, what is stored in this variable is the address of a variable. Through this address, the machine can find the value of the corresponding variable. For example, int* pa = & a means that the address of an is in the drawer of the variable pa. Its type is: int*, continue to look at the figure:

It is important to note here that the relationship between pointer pa and an is: a drawer puts a variable value of 10pa. it is the address of the variable: 0x00000001, which must be kept in mind here. It is easier to understand when quoted below.

A reference is another name for a variable

Continue to talk about references, references and pointers are often foolishly confused, because their behavior is really very weird, the effect looks very similar, look at the code:

Since the concept of reference was introduced in C++, the following code uses cased references, which is just some printing, so don't worry about it.

Int main () {

Int a = 10 pa / variable int * pa = & a; / pointer int & b = a; / / reference printf ("a:% d\ n", a); / a: 10 printf ("* pa:% d\ n", * pa); / / * pa: 10 printf ("b:% d\ n", b); / b: 10 * pa = 20

Printf ("a:% d\ n", a); / a: 20 printf ("* pa:% d\ n", * pa); / / * pa: 20 printf ("b:% d\ n", b); / / b: 20 b = 30

Printf ("a:% d\ n", a); / a: 30 printf ("* pa:% d\ n", * pa); / / * pa: 30 printf ("b:% d\ n", b); / / b: 30 a = 40

Printf ("a:% d\ n", a); / a: 40 printf ("* pa:% d\ n", * pa); / / * pa: 40 printf ("b:% d\ n", b); / / b: 40 return 0;}

Through the above code, we find that both the pointer and the reference can achieve the same effect: both have the ability to modify the value of a, as mentioned earlier, because it saves the address of a, and after dereferencing, it actually opens the drawer of a, so it can be modified. So how is the quotation done? Note one detail here: * pa = 20; c = 30 per a = 40. We see that operating c is the same way as operating a: directly using the variable name, but if pa wants to change the value of a, it must do * pa operation (dereferencing). If pa=20 directly, this is just the value of the changed pa, which points to another address.

Why do references operate in the same way as variables? Let's first take a look at the definition of a reference:

A reference is an alias for a variable, and the operation on the reference is exactly the same as on the variable directly.

So what does the alias mean?

Did you see that? An is b, b is a. The system does not allocate extra space for references, and it can even be simply interpreted as: this alias is just for programmers, and at the machine code level, they all change to the address: 0x 00000001.

There is a code as proof.

Through the above analysis, I don't know how much you understand? Or are you skeptical about pointers and references? It doesn't matter, just write some code to prove it, what we need to prove is:

Reference is an alias for a variable, so its address should be the same as that of the variable

The pointer holds the address of the variable, so its value is the address of the variable, which is different from the address of the variable.

To prove, the program is designed as follows: define a variable, assign values to pointers and references, and then check their corresponding values and addresses.

Int main () {

Int a = 10

Printf ("% d\ n", a)

Printf ("% p\ n", & a)

Printf ("~\ n")

Int * b = & a

Printf ("% p\ n", b)

Printf ("% p\ n", & b)

Printf ("~\ n")

Int & c = a

Printf ("% d\ n", c)

Printf ("% p\ n", & c)

Return 0;}

Get the output:

10 / / value of variable a 0x7ffee3c7a768 / / address of variable a ~ 0x7ffee3c7a768 / / value of pointer is the address of variable a 0x7ffee3c7a760 / / pointer variable's own address ~ 10 / / value of variable a 0x7ffee3c7a768 / / refers to the address of variable c, which is exactly the same as the address of variable a

If the pointer wants to print the value of the variable an above, you need a dereference operation: printf ("% d\ n", * b)

Summary

A variable consists of three parts: variable name, variable value, and variable address.

The variable name is actually just for the programmer, and there is no variable name in the compiled code.

A pointer variable is a variable that stores the address of another variable, and the system allocates memory space to store that address.

The reference is actually an alias for the variable, and it has the same address as the variable.

These are all the contents of this article entitled "how to use C++ variables". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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

Internet Technology

Wechat

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

12
Report