The difference between pointer and reference in C++

  • 2020-05-12 03:01:14
  • OfStack

The difference between Pointers and references in C++

Pointers and references are commonly used in C++, but the differences between them are not familiar to many beginners, so let's talk about the differences and usage between them.

1. Definitions and properties of Pointers and references:

(1) pointer: pointer is a variable, but this variable stores an address, pointing to a memory storage location; And the reference is essentially the same thing as the original variable, just a single name of the original variable. Such as:


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

int a=1;int &b=a;

Above, we define 1 plastic variable and 1 pointer variable p, which points to the storage location of a, that is, the value of p is the address of the a storage location.

The following two sentences define a plastic variable a and the plastic a reference b. In fact, a and b are the same thing, occupying the same memory location.

(2) there can be const pointer, but no const reference;

(3) Pointers can have multiple levels, but references can only be 1 level (int **p; It's legal and int &&a is not.)

(4) the value of the pointer can be null, but the value of the reference 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, it points to other storage units, while the reference will not change after initialization.

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

(7) the self-increment (++) operation of pointer and reference has the same meaning;

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

(1) pointer is passed as a parameter:


#include<iostream>
using namespace std;

void swap(int *a,int *b)
{
  int temp=*a;
  *a=*b;
  *b=temp;
}

int main(void)
{
  int a=1,b=2;
  swap(&a,&b);
  cout<<a<<" "<<b<<endl;
  system("pause");
  return 0;
}

It's 2, 1;

Passing parameters by pointer can achieve the purpose of changing argument, because the address of argument is passed, so *a is actually to fetch the data in the memory unit where the argument is stored, that is, to change the argument, so the purpose can be achieved.

Let's do another one;


#include<iostream>
using namespace std;

void test(int *p)
{
  int a=1;
  p=&a;
  cout<<p<<" "<<*p<<endl;
}

int main(void)
{
 int *p=NULL;
 test(p);
 if(p==NULL)
 cout<<" Pointer to the p for NULL"<<endl;
 system("pause");
 return 0;
}

The running result is:

0x22ff44 1

Pointer p to NULL

You might be wondering, what's going on? Isn't the address being passed? What's going on with p? NULL? In fact, a pointer p is declared in the main function and assigned to NULL, and when the test function is called, the address is actually passed, but the address is passed. So when you pass a pointer as an argument, you're actually passing a value, but you're just passing the address. When passing through the pointer as the argument, but also will be a copy of the argument passed to the parameters, namely p any of the above procedure main function test function used in p not with one variable, two variables stored p unit is not the same (only 2 point p with 1 storage unit), then modify p in test function, will not affect main p value of function.

If you want to modify at the same time, you have to use references.

2. Pass the reference as an argument to the function.

When a reference is passed as a function parameter, the argument itself is essentially passed, that is, not one copy of the argument is passed in. Therefore, the modification of the parameter is actually the modification of the argument. Therefore, when the parameter is passed by reference, not only time but also space can be saved.

Take a look at this program:


#include<iostream>
using namespace std;

void test(int &a)
{
  cout<<&a<<" "<<a<<endl;
}

int main(void)
{
 int a=1;
 cout<<&a<<" "<<a<<endl;
 test(a);
 system("pause");
 return 0;
}

The output result is: 0x22ff44 1

0x22ff44 1

Take a look at this program again:

This is enough to show that when you pass arguments with a reference, you're actually passing the argument itself, not the copy.

So if you want to modify the pointer at the same time, you have to use a reference.


#include<iostream>
using namespace std;

void test(int *&p)
{
  int a=1;
  p=&a;
  cout<<p<<" "<<*p<<endl;
}

int main(void)
{
 int *p=NULL;
 test(p);
 if(p!=NULL)
 cout<<" Pointer to the p Don't for NULL"<<endl;
 system("pause");
 return 0;
}

The output result is: 0x22ff44 1

Pointer p is not NULL

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: