Talking about the difference between pointers and references in C + +

Source: Internet
Author: User

Pointers and references are common in C + +, but for the difference between them many beginners are not too familiar with the differences and usages of their 2.

1. The definition and nature of pointers and references differ:

(1) Pointer: The pointer is a variable, except that the variable stores an address, pointing to a memory cell, whereas a reference is essentially the same thing as the original variable, but an alias of the original variable. Such as:

int A=1;int *p=&a;

int A=1;int &b=a;

The above defines an shaping variable and a pointer variable p, which points to the storage unit of a, that is, the value of P is the address of a storage unit.

and the following 2 sentences define an shaping variable A and a reference B to shape A, in fact A and B are the same thing, in memory occupy the same storage unit.

(2) can have a const pointer, but no const reference;

(3) Pointers can have multiple levels, but the reference can only be one level (int **p; legal and int &&a is illegal)

(4) The value of the pointer can be null, but the reference value cannot be null, and the reference must be initialized at the time of definition;

(5) The value of the pointer can be changed after initialization, that is, pointing to other storage units, and the reference will not change after initialization.

(6) "sizeof reference" gets the size of the variable (object) that is pointed to, and the "sizeof pointer" gets the size of the pointer itself;

(7) The pointer and the reference of the self-increment (+ +) operation meaning is different;

2. The difference between a pointer and a reference when passed as a function parameter.

(1) The pointer is passed as a parameter:

1#include <iostream>2 using namespacestd;3 4 voidSwapint*a,int*b)5 {6     inttemp=*A;7*a=*b;8*b=temp;9 }Ten  One intMainvoid) A { -     intA=1, b=2; -Swap (&a,&b); thecout<<a<<" "<<b<<Endl; -System"Pause"); -     return 0; -}

The result was 2 1;

By passing parameters with pointers, you can achieve the purpose of changing the arguments, because the address of the actual argument is passed, so the use of *a is actually the data in the memory unit that takes the stored arguments, that is, the actual parameters are changed, so you can achieve the purpose.

Look at a program again;

1#include <iostream>2 using namespacestd;3 4 voidTestint*p)5 {6     intA=1;7p=&A;8cout<<p<<" "<<*p<<Endl;9 }Ten  One intMainvoid) A { -     int*p=NULL; - test (p); the     if(p==NULL) -cout<<"pointer p is null"<<Endl; -System"Pause"); -     return 0; +}

The result of the operation is:

0X22FF44 1

Pointer P is null

People may feel strange, what is the matter, is not to pass the address, how p back to null? In fact, a pointer p is declared in the main function, and the value is null, when the test function is called, the address is actually passed, but the address is passed. That is, when the pointer is passed as a parameter, the value is actually passed, but the address is passed. When the pointer is passed as a parameter, it is also a copy of the argument is passed to the formal parameter, that is, the above program main function in the P-test function of the p is not the same variable, storing 2 variable p cell is not the same (just 2 p points to the same storage unit), The change to p in the test function does not affect the value of p in the main function.

If you want to achieve and simultaneously modify the purpose, you have to use the reference.

2. Pass the reference as a parameter to the function.

When a reference is passed as a function parameter, it is essentially passing the argument itself, that is, not a copy of the argument passed in, so the modification of the formal parameter is actually a modification of the actual parameter, so when the parameter is passed by reference, it not only saves time, but also saves space.

Look at the following procedure:

1#include <iostream>2 using namespacestd;3 4 voidTestint&a)5 {6cout<<&a<<" "<<a<<Endl;7 }8 9 intMainvoid)Ten { One     intA=1; Acout<<&a<<" "<<a<<Endl; - test (a); -System"Pause"); the     return 0; -}

The output is: 0x22ff44 1

0X22FF44 1

Look at this program again:

This is enough to show that arguments are passed by reference, actually passing the argument itself, not the copy.

So in order to achieve the purpose of modifying pointers at the same time, we have to use references.

1#include <iostream>2 using namespacestd;3 4 voidTestint*&p)5 {6    intA=1;7p=&A;8cout<<p<<" "<<*p<<Endl;9 }Ten  One intMainvoid) A { -     int*p=NULL; - test (p); the     if(p!=NULL) -cout<<"pointer p is not NULL"<<Endl; -System"Pause"); -     return 0; +}

The output is: 0x22ff44 1

Pointer p is not NULL

Talking about the difference between pointers and references in C + +

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.