c++的数组指针、指针数组、引用数组、数组引用、
2021-01-26 13:14
标签:ret 现在 ios 其他 的区别 mes 理解 ring 简单
做一个简单的小总结
注意最后面的词语,前面的是修饰
数组指针
本质是一个数组
直接看代码
#include
using namespace std;
#define size 3
typedef int(*test_ptr)[size]; //test_ptr类型 int(*)[3] 代表一个数组大小为3 类型为int的数组指针
int main()
{
int test_array[size]{1,2,3};
test_ptr array_pt = &test_array; //把test_array指针传给array_pt,现在array_pt是一个数组指针
for(int i = 0; i
图解指针数组
和普通指针比较
指针数组
根据开始的总结,指针数组是一个数组,存储的都是指针
#include
using namespace std;
int main()
{
const char* a[3]{"12", "23", "45"};
//a这个char类型的数组存储了三个指向字符串的指针
std::cout
这个比较好理解,就是存储的是指针
数组引用
根据第一个总结,这是一个引用,引用的是一个数组
#include
using namespace std;
typedef int(&array_define)[3];
int main()
{
int array[3]{1,2,3};
array_define quote = array;
//既然普通引用是变量直接赋值,那么数组引用也就是数组直接赋值
//引用之后,对数组引用的操作和对数组的操作相同
std::cout
插一句题外话,关于指针地址相加,你可以想象变量、都是地址连续的排列的,那么指针向后移动,其实就是指针的地址向前走
引用数组
根据第一句总结,是一个数组,里面是引用
这里你是不是觉得和引用指针有点像
但是!!!!
c++不支持引用数组
原因是对于数组而言,数组存储的变量地址都是连续的,但是引用是引用其他变量的,其他变量可能来自不同的地方,有不同的地址值
对引用取地址就是对变量取地址,那么这就和数组的根本定义矛盾了!!!
#include
using namespace std;
int main()
{
string& a[3] = {"2", "3", "4"};
return 0;
}
//报错
D:\Qt_test\untitled1\main.cpp:6: error: declaration of ‘a‘ as array of references
string& a[3] = {"2", "3", "4"};
^
错误:将a声明引用数组
以上就是四种容易混淆的称呼解释
最后例子总结一下
int a = 1;//普通 int
char* b = "1";//普通char类型指针
int a[3]{1,2,3}; //类型为int[3]的数组
int(aa) [3] = &a; //类型为int()[3]的指针,数组指针
char* aa[3]={"1","2","3"};//类型为int*[3]的数组,指针数组
int(&bb)[3] = a;//类型为int(&)[3]的引用,引用数组
//不存在的int& bb[3];
c++的数组指针、指针数组、引用数组、数组引用、
标签:ret 现在 ios 其他 的区别 mes 理解 ring 简单
原文地址:https://www.cnblogs.com/zero-waring/p/12776044.html
上一篇:开启 Spring 之旅:第一个 Spring 程序 ![Spring][Eclipse 使用 Spring][Eclipse 配置 Spring 约束文件][Eclipse 配置 xml 模版]
文章标题:c++的数组指针、指针数组、引用数组、数组引用、
文章链接:http://soscw.com/index.php/essay/47285.html