二维数组C ++中的指针操作 [英] 2Dimensional Array Pointer manipulation in C++

查看:125
本文介绍了二维数组C ++中的指针操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main(){
    int a[10][10];
    int **ptr =(int **)a;
    cout<<a<<endl<<ptr<<endl;
    cout<<*a<<endl<<*ptr<<endl;
    return 0;
}

在我的计算机上输出此代码

Output of this code on my computer is

0021FC20
0021FC20
0021FC20
CCCCCCCC

为什么 a 等于 * a
为什么不是* a等于* ptr?

Why is "a" equal to "*a"? why isn't *a equal to *ptr?

推荐答案


等于* a?

Why is a equal to *a?

由于您无法打印数组,因此 a int [10] [10] 隐式转换为 int(*)[10] 。因此,实际打印的而不是 a 是指向 a 的第一个

Since you cannot print an array, a is implicitly converted from int[10][10] to int(*)[10]. So what actually gets printed instead of a is a pointer to the first line of a.

* a 是数组的第一行,然后转换为指向第一个元素

*a is the first line of the array, and that in turn gets converted to a pointer to the first element.

由于数组与第一个元素具有相同的地址,所以您将获得相同的值两次。

Since an array has the same address as its first element, you get the same value twice.

这篇关于二维数组C ++中的指针操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆