In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you the "sample analysis of pointers in C++". The content is simple and clear. I hope it can help you solve your doubts. Now let the editor lead you to study and study the article "sample analysis of pointers in C++".
one。 What (what is a pointer) 1. First understanding of the address
To figure out what a pointer is, we first need to understand a concept: address.
What is the address?
We know that the memory of the computer is a continuous block of storage space. In order to improve the efficiency of CPU in finding data, we number this continuous storage space in bytes, and each byte corresponds to a number, which is called an address.
To give you an example of an image:
If we think of a dormitory as memory, then the room in the dormitory can be seen as every byte of memory, and the house number of each room can be seen as an address.
Why do you need an address?
Through the above example, we can easily understand the meaning of the existence of the address. Just imagine, if the rooms in the dormitory building do not have a house number, it will be difficult for us to describe the specific location of a specified room. The same is true of CPU's handling of memory. If there is no address, CPU has no way to determine the specific location of the specified memory, thus correctly and quickly accessing the memory space.
two。 Pointer concept
The pointer is the address.
The pointer is the address.
The pointer is the address.
The important thing is said three times, we must know one thing clearly, the pointer is the address, it can be understood that the pointer is a special name for the address in C language.
# include int main () {int a = 10; int * p = & a; / / p is a pointer? Printf ("% p\ n", p); / /% p can format the output address. Return 0;}
In that case, is p in the above code a pointer?
We said that the pointer is the address, and is p the address? Of course not! An address is a single piece of data, like int a = 10; can we say that an is 10? Of course not, 10 is a shaping constant, and an is a shaping variable. We can only say that the value stored in an is 10, not simply that an is 10.
Similarly, p in the above code is a variable with the address of a, which we call a pointer variable. So strictly speaking, p is not a pointer, but a pointer variable. Just as an is not an integer 10, but a shaping variable.
So what is a pointer? let's run this code:
Yes, strictly speaking, this number is really called a pointer (address values are usually displayed in hexadecimal in computers).
3. Pointer and pointer variable
Through the above explanation, I believe you can understand that the pointer is an address, is a constant, and the pointer variable is a variable, in which the pointer is stored, there is an essential difference between the two.
Seeing this, I believe some students will have doubts: obviously in class, or in some books, pointer variables are collectively referred to as pointers, just like p in the above example is usually called a pointer, why now it is said that p is not a pointer?
Please note that I say p is not a pointer but in a strict sense.
In our daily use, we often confuse pointers with pointer variables. This seems to be a habit, and even I may express pointer variables as pointers in the following statement.
The conclusion is: in our daily use, we can blur the concept of pointer and pointer variable, and collectively call pointer variable as pointer, but we must be clearly aware of the essential difference between pointer and pointer variable. and whether this thing you call is a pointer or a pointer variable.
two。 Why (Why should there be pointers)
After the above explanation, I believe that this problem is very simple. The pointer is the address, so why should there be an address? The reason is that in order to improve the search efficiency.
Imagine that if there is no address, when we write int a = 10;, the system will automatically open up a 4-byte space in memory to store the value of 10. When we visit CPU, because there is no address, we can only look up each other in memory, and with the address, memory can tell CPU,CPU the address value and locate it directly to the memory according to the address value, which greatly improves the efficiency and accuracy of CPU accessing memory.
three。 How (how to use pointers) 1. Basic definition
The definition format of the pointer variable is relatively simple, as long as you add the * operator between the type and the variable name, you can define the pointer variable of that type.
Int * a = NULL; / / define the shaping pointer variable double * d = NULL; / / define the double precision floating point pointer variable struct stdnt * ps = NULL; / / define the structure pointer variable
Note: NULL is a macro definition commonly used when pointer variables are initialized, calling it a null pointer and essentially a value of 0.
If the definition is not initialized, such as:
Int * a
We know that if the variable is not initialized, the value in it is random, that is, the random value is stored in the pointer variable an at this time. If the variable an is accessed at this time, the corresponding memory will be accessed with this random value as the address. This operation is illegal. This uninitialized pointer is called a wild pointer.
Therefore, in order to avoid the occurrence of wild pointers, we try to initialize pointer variables when defining them.
two。 Take address operator &
We use pointers to use addresses, so where does the address come from? Can we write code like this:
Int * a = 0x11223344
We think of 0x11223344 as an address value and give it to the shaping pointer variable a to store. There is no problem with grammar, but it makes no sense to write like this! Because the 0x11223344 address is unknown to us, we do not have read and access permissions, and the a variable at this time is equivalent to a wild pointer.
In fact, our address is taken from the & take address character.
# include int main () {int a = 10; int * p = & a; return 0;}
Here we define the integer variable a, take the address character, take the address of a, and assign it to the shaping pointer variable p.
There is a question here: an is an integer variable that takes up 4 bytes of space, and through the above introduction, we know that the address number of memory is in bytes, which means that variable an actually has four addresses. So which address did & a take out?
Come to the conclusion:
In C language, for any type of variable or array address, always get the address that is the lowest in value.
Let's draw a memory distribution map of the above code:
Assuming that the address rises from left to right, then p stores the address of the lowest byte in the 4 bytes of variable a.
3. Dereference operator *
As we mentioned above, getting the address of the variable is the lowest byte, that is, the first byte address, so how do we access the data in the original variable through the address of this byte? here we need to use the * operator:
# include int main () {int a = 10; int * p = & a; * p = 20; / equivalent to a = 20 printf ("% d\ n", * p); printf ("% d\ n", a); return 0;}
Looking at the code above, we assign the address of the a variable to the pointer variable p, and the contents of the variable can be accessed through * p.
So how exactly does the * operator work? here's the conclusion:
Dereferencing the pointer is the target the pointer points to.
Like in the example above, what is * p? * p means "a", "p" means "a", "p" means "a", and the important things are said three times. So, if I change the value of * p, it's completely equivalent to directly changing the value of the a variable.
The above code runs as follows:
So, let's look at the following code:
Int main () {int a = 0x11223344; int * p = & a; printf ("0x%x\ n", * (char*) p); return 0;}
Note that we say * p is a, but here * p is forced to convert to a char* type before dereferencing, where p is a pointer of a character type, because the character char type occupies only one byte in C, and dereferencing can only access one byte of content. We said that what is stored in p is the address of the first byte of the a variable, that is, the address of 44 (as to why, let the reader know the contents of the size end), dereference only accesses 44:
4. Structure pointer
The following defines a structure:
Typedef struct Student {int age; char name [20]; char sex [2];} STD
Any data type in C language has its own pointer type, and the structure is no exception, as follows:
Int main () {STD s = {18, "Zhang San", "male"}; STD * ps = & s; / / define structure pointer printf ("% s\ n% d\ n% s\ n", s.name, s.age, s.sex);}
We define a structural variable and initialize it, through. Operators can easily access the contents of the structure. So how do we access the contents of the structure through the structure pointer variable ps?
We said that dereferencing the pointer is the target the pointer points to, so ask the reader to think, what is * ps? Yes * ps is the s variable. In that case, we can visit it like this:
Printf ("% s\ n% d\ n% s\ n", (* ps) .name, (* ps) .age, (* ps) .sex); / / Note: because. Operators take precedence over * operators, so (* ps) must be parenthesized
The C language may find it too troublesome to write this, so we can use the-> operator to access the contents of the structure directly for the structure pointer:
Printf ("% s\ n% d\ n% s\ n", ps- > name, ps- > age, ps- > sex)
The writing of ps- > name is completely equivalent to (* ps) .name.
Note: the-> operator is used only when the structure pointer variable accesses the structure member.
5. Multistage pointer
First of all, let me ask you a question: is the pointer variable a variable? It's a variable. Since it is a variable, then there is an address, and we can still store the address of the pointer variable into a new variable, which we can call a secondary pointer variable:
Int a = 10 pa * pa = & a setint * * ppa = & pa; / / define the secondary pointer variable * ppa; / / equivalent to pa**ppa; / / equivalent to * pa, that is, equivalent to a
Secondary pointer we do not look at how mysterious, the so-called secondary pointer is actually a pointer variable, but the address of another pointer variable is stored in it.
In that case, is the secondary pointer variable a variable? Can it take the address and store it in another third-level pointer variable? So, is the third-level pointer variable a variable? If you want, you can go on like this.
6. Naming conventions for pointer variables
The reason why we separate this piece is that a good naming convention for pointer variables not only helps to improve the readability of the code, but also helps us to understand the code of some complex data structures.
The naming of the pointer is customary to precede the original variable name with the letter p. If you define the shaping variable a, its pointer variable is named pa, and the corresponding pointer variable of the definition structure variable std is named pstd. If the address of the pstd is stored in the secondary pointer variable, then the secondary pointer variable should be named ppstd.
four。 Some of my understanding of pointers
It is said that the pointer is the soul of the C language, so what is the charm of the pointer?
Let's take a look at this code that exchanges two numbers:
# include void Swap (int a, int b) {int t = a; a = b; b = t;} int main () {int a = 10; int b = 20; Swap (a, b); printf ("a =% d\ n", a); printf ("b =% d\ n", b); return 0;}
We should know that this code cannot exchange a, b, because a, b in Swap () is just a copy of a, b in the main () function. That is, the change of an and b in the Swap () function will not affect the main () function. This way of passing parameters is called passing values.
But here's what we write:
# include void Swap (int * pa, int * pb) {int t = * pa; * pa = * pb; * pb = t;} int main () {int a = 10; int b = 20; Swap (& a, & b); printf ("a =% d\ n", a); printf ("b =% d\ n", b); return 0;}
Main () passes the address of a, b when passing parameters, the Swap () function receives it with a pointer, and internally uses dereferencing to exchange, so we can complete the real exchange function. This way of passing parameters is called address.
I read in an article that it is an understanding of the nature of the pointer:
To pass a value is to pass a value.
The address is the value itself.
I hope you can deeply understand the two concepts of value and value itself.
Like our first way of writing the Swap () function, the main () function just passes the value of a, b to the Swap () function for processing, but no matter what the Swap () function does to it, it still can't change the original a, b value.
In the second way of writing Swap (), the main () function passes the address of a, b, which is equivalent to a, b itself, so that Swap () can directly change the original value of a, b when processing.
The above is all the content of this article "sample Analysis of pointers in C++". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.