指针数组退化成多级指针问题


	char const * str[3] = { "stra","strb","strc" };
	char const *p = str[0];
	int i = 0;
	i = 0;
	while (i < 3)
	{
		printf("%s\n", p++);
		++i;
	}

打印出来

stra
tra
ra

如果我们想打印出来
stra strb strc
怎么打印呢
法1:

int i = 0;
	i = 0;
	while (i < 3)
	{
		printf("%s\n", str[i]);
		++i;
	}

但是我们不想这样操作,我们想通过指针移动来操作
我们移动的是二级指针

int i = 0;
auto deal = str;
while (i < 3)
	{
		printf("%s\n", *(deal++));
		++i;
	}

但是我们来看这个类型

    auto deal = str;
	cout << typeid(deal).name() << endl;
	cout << typeid(str).name() << endl;
char const * *
char const * [3]

很明显一个是数组指针一个是二级指针,可见我们在使用时将数组指针自动退化成了二级指针。

使用数组指针行不行呢?
不行
如果我们这样子初始化这个指针

char const * res[3] = str;

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/flf1234567898/article/details/107879456