About the difference between pointer * in C language and & reference in C++

The & symbol acts as a reference under certain circumstances in C++, and a reference is an alias for an existing variable that must be initialized

The pointer can be empty or not initialized.

Below is the test code

#include <iostream>
 
using namespace std;
 
int main()
{
	//这里引用有一部分就是起别名  若对别名进行引用其实就是对变量本身引用 
    int a = 88;
    int &c = a;  //声明变量a的一个引用c,c是变量a的一个别名,如果引用,声明的时候一定要初始化
    int &d = a;  //引用声明的时候一定要初始化,一个变量可以有多个引用
    
    cout<<"a="<<a<<endl;
    cout<<"c="<<c<<endl;
    cout<<"====================="<<endl;
    c=99; //如这里是对别名c的修改  其实就是对变量a的修改   修改过以后都会变   此时a为99 
    cout<<"a="<<a<<endl;
    
    int &e = ++d;
	cout<<"a="<<a<<endl; //此时输出a的值为100 
    
    return 0;
}


& (reference) ==> is used to pass values, and when it appears on the left side of the variable in the variable declaration statement, it means that the declaration is a reference. 

& (address operator) ==> is used to obtain the first address, it appears on the right side of the equal sign when assigning an initial value to a variable or appears as a unary operator in an execution statement to indicate the address of the object
                  .

All in all, what goes with a type is a reference, and what goes with a variable is an address

Examples are as follows: 1) The reference is on the left side of the assignment =, and the address is on the right side of the assignment, such as

int a=3;
int &b=a; //reference
int *p=&a; //take address


  2) The reference is with the type, and the address is with the variable. The example is the same as above, and the following example:

int function(int &i)
{
}  //引用


3) For vector, the above two items are also suitable

vector<int> vec1(10,1);  //initialize vec1: 10 elements, every element's value is 1
vector<int> &vec2 = vec1; // vec2 is  reference to vec1
vector<int> *vec3 = &vec2; //vec3 is addresss of vec1 and vec2

--Pointer-For a type T, T* is a pointer type pointing to T, that is, a variable of type T* can store the address of a T object, and type T can add some qualifiers, such as const, volatile, etc. wait. See the figure below, the meaning of the pointer shown

char c = 'a';

char *p = &c; //p stores the address of c

--Reference-Reference is an alias of an object, mainly used for function parameters and return value types, and the symbol X& represents a reference of type X. See the figure below for the meaning of the references shown:

int i=1;

int &r = i; //i=r=1 at this time;

If execute r=2;//At this time i=r=2;

int *p = &r; // p-oriented r;


The difference between pointers and references

1. First of all, the reference cannot be null, but the pointer can be null. I also said before that a reference is an alias of an object, and the reference is empty-the object does not exist, how can there be an alias! Therefore, when defining a reference, it must be initialized. So if you have a variable that is used to point to another object, but it might be null, then you should use a pointer; if the variable always points to an object, ie, your design does not allow the variable to be null, then you should Use citations. As shown in the figure below, if you define a reference variable without initialization, it will not even compile (compile-time error)

And the declaration pointer can not point to any object. It is for this reason that a null operation must be done before using the pointer, but the reference does not.

2. Secondly, the reference cannot change the point, and "until death" to an object; but the pointer can change the point to point to other objects. Explanation: Although the reference cannot change the pointer, it can change the content of the initialized object. For example, as far as the ++ operation is concerned, the operation on the reference directly responds to the pointed object instead of changing the pointing; while the operation on the pointer makes the pointer point to the next object instead of changing the content of the pointed object.

3. Again, the size of the reference is the size of the variable pointed to, because the reference is just an alias; the pointer is the size of the pointer itself, 4 bytes

4. Finally, references are safer than pointers. References are safe because there are no null references, and once a reference has been initialized to point to one object, it cannot be changed to a reference to another object. For a pointer, it can point to another object at any time, and it can be uninitialized or NULL, so it is not safe. Although const pointers cannot be changed, there are still null pointers, and wild pointers may be generated (that is, multiple pointers point to a piece of memory, and after freeing one pointer, other pointers become wild pointers)

In short, it can be summed up in one sentence: a pointer points to a piece of memory, and its content is the address of the pointed memory; while a reference is an alias of a certain piece of memory, and the reference does not change the pointing.

This article is a reprint of the original article of CSDN blogger "Situ Ruohan", following the CC 4.0 BY-SA copyright agreement

https://blog.csdn.net/qq_26501341/article/details/58192662

Guess you like

Origin blog.csdn.net/weixin_49418695/article/details/123323926