In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
The knowledge points of this article "C++ pointer variable value and address method" are not quite understood by most people, so the editor summarizes the following contents to you. The content is detailed, the steps are clear, and it has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "C++ pointer variable transfer value and addressing method" article.
1 introduction
For example: after the func function exits, the pointer pInt refers to a content * pInt of 12
# include
/ / official account: C language and CPP programming
Int func (int* pRes)
{
If (pRes = = NULL)
PRes = new int (12); / / allocate new memory space to the pointer pRes and assign a value
Return 0
}
Int main ()
{
Int * pInt = NULL
Int val = func (pInt)
Printf ("% d\ n", * pInt); return 0
}
Parsing: the formal parameter of the int func (int* pRes) function is the pointer type int* pRes, which new a piece of memory in the function body and assigns a value of 12, assigning the memory address to the pointer pRes. In the main function, the pointer pInt is defined, the func function is called, and pInt is passed into the func function as an argument. As a result, * pInt is not 12.
Reason: in the process of calling the func function, the formal parameter and the actual parameter use the value transfer mode, in this case, the formal parameter variable changes in the function body, after the end of the function, the formal parameter variable is released, and the result of the change cannot be returned to the actual parameter.
You can use pointer passing or reference passing. To change the value of pRes in the body of a function and return this change to the main function, you must pass a pointer to pRes. Because pRes itself is a pointer, you should pass a pointer to the pointer, or a reference to the pointer.
Reference to the pointer int v = 1
Int * p = & v *'
Int * & rp = p
& indicates that r is a reference. * make sure that the type of r reference is a pointer.
Because a reference is not an object, there is no array of references, no pointers to references, no references to references:
Int& a [5]; / / error
Int&* p; / / error
Int& & r; / / error
So modify the function int func (int* pRes); to int func (int* & pRes)
# include
Int func (int* & pRes)
{
If (pRes = = NULL)
PRes = new int (12); / / allocate new memory space to the pointer pRes and assign a value
Return 0
}
Int main ()
{
Int * pInt = NULL
Int val = func (pInt)
Printf ("% d\ n", * pInt)
Return 0
}
(2) differences and relations between passing values and quoting.
Pass value: a copy of the argument is passed to the parameter. Is to assign the parameter to the parameter, after the assignment, the parameter does not have any relationship with the parameter, the modification of the parameter will not affect the parameter.
Pass address: pass a copy of the argument address to the parameter. Is to copy the address of the argument to the parameter. After copying, there is no relationship between the address of the parameter and the address of the parameter, and the modification of the address of the parameter will not affect the parameter, but the modification of the object pointed to by the address of the parameter is directly reflected in the argument, because the object that the parameter points to is the object of the parameter.
Pass reference: there is essentially no copy of any arguments, and two variables point to the same object. This is a modification of the parameter, which is bound to be reflected in the actual parameter.
No matter whether you pass a value or a pointer, the function generates a temporary variable, but when passing a reference, it does not generate a temporary variable. When passing a value, you can only refer to the value and not change the value, but when you pass a value reference, you can change the value. When you pass a pointer, you can only change what the pointer refers to, not the pointer itself, but when you pass a pointer reference, you can change both the content of the pointer and the pointer itself. Referencing the parameters of the transfer function does not produce a copy of the argument in memory, it operates directly on the argument When a function call occurs, the formal parameter needs to be assigned a storage unit, and the formal parameter variable is a copy of the argument variable; if the object is passed, the copy constructor will be called. Therefore, when the data passed by the parameter is large, it is more efficient and takes up more space to pass the parameter with reference than with general variable. The above is the content of this article on "the value and address method of C++ pointer variable". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related 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.
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.