[C ++ Basics] 019 _ pointer and reference (int *, Int &, int * &, Int & *, int **)

Source: Internet
Author: User

I believe many cppers, like me, have been hovering in pointers and references for a long time. We often mix pointers and references, especially when we see the usage listed in the question, which really hurt our brains.

Later, I used it in the project to find out the difference. However, for a programmer, the most taboo is that I don't know why, and I will use and understand it in two ways. People who stay at the meeting will always turn around in the same place. People who know the same thing can give a different picture.

This article introduces the pointer and Reference Usage of the question.

First, let's popularize the concept and describe what are pointers and references. Here we use the variable name as the transition.

■ Pointer ---- refers to a memory variable. The information stored in the pointer is the first address of the memory address. Its type determines the size of the memory. In comparison, store lockers in supermarkets. I remember the third cabinet in the first row and the third cabinet in the right. here 1-3 is an address. Through the address, I can find the cabinet. The address here is a pointer. ■ Variable-the name of a memory address data value is similar to that of a supermarket locker. I remember that there was a sticker on the cupboard where I put my package. It says 18. I can find it through 18. Here 18 is the variable name. ■ Reference ---- alias of the variable, the store locker of the supermarket. The sticker on the cabinet says 18, but 18 is not easy to remember. I use a homophone to remember 18 as "sending". I can also find the Cabinet through "sending. Here, "to publish" is a reference.

We can see from the above metaphor that there are three methods to find a cabinet:

1. The third address (pointer) from the right of the first row 2. variable name 18 on the cabinet sticker 3. "To publish" Reference

 

After understanding the pointer and reference concepts, let's take a look at their usage. Here we take the classic value exchange as an example, int, int *, Int &, int * &, Int & *, int **.

1. int

 1 #include <iostream> 2 using namespace std; 3  4 void swap1(int a, int b){ 5     int tmp; 6     tmp = a; 7     a = b; 8     b = tmp; 9 }10 int main(){11     int a = 1;12     int b = 2;13     swap1(a, b);14     cout<<"a = "<<a<<endl;15     cout<<"b = "<<b<<endl;16     system("pause");17     return 0;18 }

Exchange failed. Students with a strong foundation can see at a glance that such an exchange only exchanges the values of the form parameters and cannot achieve the exchange value. This procedure is very simple and will not be detailed.

 

2. int *

#include <iostream>using namespace std;void swap2(int *a, int *b){    int tmp;    tmp = *a;    *a = *b;    *b = tmp;}int main(){    int a = 1;    int b = 2;    swap2(&a, &b);    cout<<"a = "<<a<<endl;    cout<<"b = "<<b<<endl;    system("pause");    return 0;}

Exchange successful. Swap2 accepts the address as the parameter above. If we pass in the address, we can directly operate on the value of the real parameter. This is also very simple and will not be detailed.

 

3. Int &

#include <iostream>using namespace std;void swap3(int& a, int& b){    int tmp;    tmp = a;    a = b;    b = tmp;}int main(){    int a = 1;    int b = 2;    swap3(a, b);            cout<<"a = "<<a<<endl;    cout<<"b = "<<b<<endl;    system("pause");    return 0;}

Exchange successful. Reference is an alias. You can directly access real parameters and control real parameters through reference.

4. int *&

 1 #include <iostream> 2 using namespace std; 3  4 void swap5(int *&a, int *&b){ 5     int tem = *a; 6     *a = *b; 7     *b = tem; 8 } 9 10 int main(){11     int a = 1;12     int b = 2;13 14     int *aPtr = &a;15     int *bPtr = &b;16     int *&arPtr = aPtr;17     int *&brPtr = bPtr;18 19     swap5(arPtr, brPtr);20 21     cout<<"a = "<<a<<endl;22     cout<<"b = "<<b<<endl;23     system("pause");24     return 0;25 }

Exchange successful. This is a little more complicated. For int * & Value declaration, we can see from the symbol closest to value as a &, indicating that value is a reference. What is it a reference? Again, * is a pointer reference, that is, the alias of the pointer. We can use * value to access the value of the real parameter. Therefore, the internal logic of the SWAp function is the same as that of int.

 

5. Int &*

Exchange successful. This definition is not legal. If we analyze it literally, for example, Int & * value, which is closest to value *, it indicates that value is a pointer. Then we can look at the previous one &, it indicates that a pointer is referenced, and a pointer cannot be referenced. Therefore, such a definition is invalid. Do not remember this definition.

 

6. Int **

1 # include <iostream> 2 using namespace STD; 3 4 void swap6 (INT ** A, int ** B) {5 Int TMP; 6 TMP = **; 7 ** A = ** B; 8 ** B = TMP; 9} 10 11 int main () {12 int a = 1; 13 int B = 2; 14 int * aptr = & A; // pointer to data 15 int * bptr = & B; // pointer to data 16 int ** aaptr = & aptr; // The pointer to the pointer address 17 int ** bbptr = & bptr; // the pointer to the pointer address 18 swptr (aaptr, bbptr ); 19 cout <"A =" <A <Endl; 20 cout <"B =" <B <Endl; 21 system ("pause "); 22 return 0; 23}

Exchange successful. Similarly, for example, int ** value, which is closest to value * indicates that value is a pointer and the previous one is *. It indicates a pointer to a pointer. This is legal, how can we access the actual parameter value represented by value? It's easy to use ** value. Remember that * is an operator, just like &, but "&" is an address operator, and "*" is a value operator.

Related Article

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.