In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the example analysis of pointer reference in C++, the article is very detailed, has a certain reference value, interested friends must read it!
Pointers and references are quite different in form, but they seem to have the same function, and both can reference objects directly and manipulate them directly.
First, the reference cannot be empty, but the pointer can be empty. As mentioned earlier, a reference is an alias for an object, and the reference is empty-- the object does not exist, how can there be an alias! Therefore, when defining a reference, it must be initialized. So if you have a variable that points to another object, but it may be empty, you should use a pointer; if the variable always points to an object, i.e., your design does not allow the variable to be empty, then you should use a reference.
Like pointers, references to pointers can be confusing.
We noticed syntax similar to the following
Void func (int * & x) {+ + x;}
I guess you might be a little confused about int * & x.
This is called a reference to a pointer.
Int * & x
Don't think it looks complicated, but it's not complicated at all.
Let me take a look at it for you:
According to the habit of C++ programmers, the pointer "*" is placed with the type.
In C++ & are quotation symbols.
What we need to note is that "reference" does not produce a copy, but aliases the original variable.
A reference operation is an operation on the original variable.
So all you have to do is:
Int* & x
Be manifest at a glance!
Modifications to the pointer variable itself cannot be applied to the original pointer variable
So you need to modify the pointer variable by reference.
I'll use two pictures to show you why the pointer reference is useful:
What is partial modification?
Take a chestnut.
I'll use the code to explain to you what partial modification is:
# include void swap (int* p1Magnette * p2) {int* temp=p1; p1jewelry p2; p2traintemp; printf ("in switching: aversion% dcenture baked% d\ n", * p1reagin p2); printf ("switching (address): p1 percent d\ n", p1); printf ("exchanging (address): p2percent% d\ n", p2);} int main () {int astat1 category bread3; int* p1transactions p2roomb / / pre-exchange printf ("pre-swap: aversion% ddirection billing% d\ n", * p1scoop p2); printf ("pre-swap (address): p1century% d\ n", p1); printf ("pre-swap (address): p2percent% d\ n", p2); / / swap in the exchange (p1Powerp2) / / printf after exchange ("after exchange: aversion% d recording% d\ n", * p1jingle p2); printf ("after exchange (address): p1 percent d\ n", p1); printf ("after exchange (address): p2 percent d\ n", p2); return 0;}
Guess the result.
Output result:
Before the exchange: axiom 1, dint 3
Before switching (address): p1q6422028
Before switching (address): p2q6422024
In the exchange: axiom 3
In exchange (address): p1q6422024
Switching (address): p216422028
After the exchange: axiom 1, dint 3
After exchange (address): p1q6422028
After exchange (address): p2q6422024
Run screenshot
When the swap () function is executed, it is executing.
You can see that the address was actually changed when the swap () function was executed, and the values of an and b were exchanged.
However, when we output an and b in the main () function, there is no exchange at all.
What is global modification?
The same code, I only change one place.
Let's see what happens.
Run screenshot
Before the exchange: axiom 1, dint 3
Before switching (address): p1q6422044
Before switching (address): p216422040
Exchanging: axiom 3, Bethle1
In exchange (address): p1q6422040
Switching (address): p216422044
After the exchange: axiom 3, dint 1
After exchange (address): p1room6422040
After exchange (address): p2q6422044
I simply added "&", which is so amazing!
So we can find out:
Pointer references can modify pointer variables globally!
References are a powerful syntax in C++ and are extremely useful in programming.
It is critical to understand this syntax because it is widely used in tree and graph algorithms.
The above is all the content of this article "sample Analysis of pointer references in C++". Thank you for reading! Hope to share the content to help you, more related 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.