Here you will learn about difference between reference and pointer in C++.
在这里,您将了解C ++中引用和指针之间的区别。
Reference: Reference contains address of a variable. It can be called as alternative name of a variable.
引用:引用包含变量的地址。 可以将其称为变量的备用名称。
Example:
例:
int i;
int &ri = i;
Pointer: Pointer also contains address of a variable.
指针: 指针还包含变量的地址。
Example:
例:
int i;
int *pi = &i;
Both reference and pointer are used to point to address of a variable but they are different in many ways.
引用和指针都用于指向变量的地址,但是它们在许多方面都不同。
C ++中引用和指针之间的区别 (Difference between Reference and Pointer in C++)
S.No | Reference | Pointer |
1. | Once reference is assigned an address then it can’t be reassigned address of any other variable. | There is no such restriction. |
2. | Reference can’t be NULL. | Pointer can be NULL. |
3. | A reference should be initialized at the time of its declaration. | There is no such restriction. |
4. | We can dereference the address without using any operator. | In pointer, address is dereferenced using * operator. |
5. | We can’t increment or decrement the address stored in reference. Due to this, reference is safe to use. | It is possible to increment or decrement the address stored in pointer. Due to this, pointer is not safe to use. |
序号 | 参考 | 指针 |
1。 | 一旦给引用分配了地址,则不能将其重新分配给任何其他变量的地址。 | 没有这样的限制。 |
2。 | 引用不能为NULL。 | 指针可以为NULL。 |
3。 | 引用应在声明时进行初始化。 | 没有这样的限制。 |
4。 | 我们可以取消引用地址,而无需使用任何运算符。 | 在指针中,使用*运算符取消引用地址。 |
5, | 我们不能增加或减少引用中存储的地址。 因此,引用是可以安全使用的。 | 可以增加或减少指针中存储的地址。 因此,指针不安全使用。 |
Comment below if you found any mistake or know any other difference between pointer and reference in C++.
如果发现任何错误或知道C ++中的指针和引用之间有任何其他区别,请在下面评论。
翻译自: https://www.thecrazyprogrammer.com/2016/12/difference-between-reference-and-pointer.html