C ++中引用和指针之间的区别

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;
Difference between Reference and Pointer in C++

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.NoReference

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值