Detailed description of the pointer and reference of the c ++ pointer

Source: Internet
Author: User

Detailed description of the pointer and reference of the c ++ pointer
It shows the pointer passed to the method by using pointer and pointer reference, so as to better use it. (The pointer here is not a two-dimensional array.) Why do we need to use them? When we pass a pointer as a parameter to a method, we actually pass a copy of the pointer to the method, it can also be said that the pass pointer is the value transfer of the pointer. If we modify the pointer inside the method, a problem occurs. Modifying the pointer in the method only changes the copy of the pointer rather than the pointer itself. The original pointer retains the original value. The following code describes the problem: copy the code int m_value = 1; void func (int * p) {p = & m_value;} int main (int argc, char * argv []) {int n = 2; int * pn = & n; cout <* pn <endl; func (pn ); cout <* pn <endl; return 0 ;} copy the code. The output result is two pointers with pointers. the pointer is used as the parameter to copy the code void func (int ** p) {* p = & m_value; // You can also allocate the memory * p = new int; ** p = 5;} int main (int argc, char * argv []) based on your needs. {int n = 2; int * pn = & n; cout <* pn <endl; func (& p N); cout <* pn <endl; return 0;} copy the code. Let's take a look at the func (int ** p) Method p: a pointer, here we will not modify it, otherwise we will lose the pointer address pointed to by this pointer * p: It is the pointer pointed to, it is an address. If we modify it, the content of the pointer to be modified. In other words, we modified the * pn pointer ** p in the main () method: the two unreferences point to main () in the * pn content pointer reference method, let's look at the reference code of the pointer and copy the code int m_value = 1; void func (int * & p) {p = & m_value; // You can also allocate the memory p = new int; * p = 5;} int main (int argc, char * argv []) {int n = 2; int * pn = & n; cout <* pn <endl; func (pn); cout <* pn <endl; return 0 ;} copy the code to check whether the func (int * & p) Method p is a pointer reference. * pn * p in the main () method is main () method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.