当前位置:网站首页>02-3、指针和引用的区别
02-3、指针和引用的区别
2022-07-17 14:45:00 【列宁格勒的街头】
前言:本文章主要用于个人复习,追求简洁,感谢大家的参考、交流和搬运,后续可能会继续修改和完善。
因为是个人复习,会有部分压缩和省略。
指针和引用的区别
int main()
{
int a = 10;
int& ra = a;//在语法上,这里给a这块空间取了一个别名,没有新开空间
ra = 20;
int* pa = &a;//在语法上,这里定义了一个pa指针变量,开了4个字节存储a的地址
*pa = 20;
return 0;
}实际从汇编实现角度,引用的底层也是类似指针存地址的方式处理

在使用引用和指针时,它们的区别:
1.引用概念上定义一个变量的别名,指针存储一个变量地址。
2.引用再定义时必须初始化,指针没有要求。
3.引用在初始化时引用一个实体后,就不能再引用其他实体,而指针可以在任何时候指向任何一个同类型实体。
4.没有NULL引用,但有NULL指针。
5.在sizeof中含义不同:引用结果为引用类型的大小,但指针始终是地址空间所占字节个数。
6.引用自加即引用的实体增加1,指针自加即指针向后偏移一个类型的大小
7.有多级指针,但是没有多级引用。
8.访问实体方式不同,指针需要显示解引用,引用是编译器自己处理
9.引用比指针使用起来相对更安全
边栏推荐
- 搭建OpenStack-M版的Cinder所碰到过的状况
- Resources for Physics based simulation in Computer Graphics 图形学中物理模拟的资源整理
- Unity Dropdown(可编辑,可输入)下拉选择框,带文本联想
- 机器人开发--常用仿真软件工具
- Cmake common commands (V)
- Unity dropdown (editable, inputable) drop-down selection box with Text Association
- The basic establishment of the sequence table and the related operations of adding, deleting, modifying and querying (the sequence table described in C language)
- OA系统与MES系统的异同点
- Delegate parents and other loaders
- Powercli script performance optimization
猜你喜欢

如何在 RHEL 9 中更改和重置忘记的root密码

LeetCode 745. 前缀和后缀搜索

Unity dropdown (editable, inputable) drop-down selection box with Text Association

常用getshell工具的下载

Today's sleep quality record 79 points
![Some methods of early MCU encryption [get data in the comment area]](/img/14/8e1dcb799d8a3c0aefcac09be9dc51.png)
Some methods of early MCU encryption [get data in the comment area]

Conversion of unity3d model center point (source code)

Mysql优化系列之limit查询

Leetcode 1328. 破坏回文串(可以,已解决)

Powercli script performance optimization
随机推荐
LOJ 2324 - "Tsinghua training 2017" small y and binary tree
Introduction to replacement technology of SAP ABAP CDs view view
Accident caused by the combination of annotation transaction + distributed lock at the read committed level -- disorder of activity purchase opportunities
MySQL autoincrement ID, UUID and snowflake ID
function/symbol ‘pango_context_set_round_glyph_positions‘ not found in library ‘libpango-1.0.so.0‘x
ThreadLocal变量使用及原理
Leetcode 1304. 和为零的 N 个不同整数
Configuration of vscode+unity3d
机器人开发--机器人资料汇总
Performance optimization @contented to reduce pseudo sharing
NVIDIA uses AI to design GPU: the latest H100 has been used, which reduces the chip area by 25% compared with traditional EDA
TS solves the problem that the type file of the imported plug-in does not exist
Mysql 自增id、uuid与雪花id
Similarities and differences between OA system and MES system
Sword finger offer II 041 Average value of sliding window
Leetcode 1252. 奇数值单元格的数目
Codeforces - 587e (linear basis + segment tree + difference)
Discussion on Euler angle solution of rocket large maneuvering motion
公网连接MySQL实例的解决方案
Tier defect detection using full revolutionary network