内存中2D阵列的阻碍 [英] Implemantation of 2D array in Memory

查看:64
本文介绍了内存中2D阵列的阻碍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码将输出显示为:-

地址:3213360604值:3213360604
地址:3213360604值:0


The following code gives the output as :-

Address : 3213360604 Value : 3213360604
Address : 3213360604 Value : 0


int main()
{
    unsigned int a[3][3] = {};

    printf("Address : \t%u \tValue : \t%u\n", &a[0], a[0]);
    printf("Address : \t%u \tValue : \t%u\n", &a[0][0], a[0][0]);

    return 0;
}



不同的值如何存储在相同的地址中?



How can different values be stored in the same address?

推荐答案

取消引用数组名(a[0],如a是数组名),得到的结果与数组名本身相同,即数组的基地址.这是C(和C++)的众所周知(奇怪?!)功能.
Because dereferecing an array name (a[0], like a, is an array name), gives the same result then the array name itself, i.e. the base address of the array. This is a well know (weird?!) feature of C (and C++).


您期望a [0]和a [0,0]同样合法检索数组的第一个元素的值不正确.

因为您使用的是多维数组,所以当您使用一维语法引用它时,您不会在数组中获得正确的条目,而是在获取数组第二维的起始地址.两个实验将阐明这一点.

首先,不要打印出该值,而是尝试使用它并查看收到的错误消息.
Your expectation that a[0] and a[0,0] are equally legal to retrive the value of the first element of the array is incorrect.

Because you are using a multidimensional array, when you reference it using a single dimension syntax, you are not getting entries in the array proper, you are getting the address of the start of the 2nd dimension of the array. Two experiments will clarify this.

First, rather than print out the value, attempt to use it and watch the error message you receive.
unsigned int foo = a[0];

error C2440: ''initializing'' : cannot convert from ''unsigned int [3]'' to ''unsigned int''



其次,更改程序以打印其他索引,这将向您显示a [0],a [1]和a [2]是遍历数组的指针,而不是数组本身中的数据. >



Second, change the program to print out the other indices, this will show you that a[0], a[1], and a[2] are pointers marching through the array, not the data in the array itself.

printf("Address : \t%u \tValue : \t%u\n", &a[0], a[0]);
printf("Address : \t%u \tValue : \t%u\n", &a[1], a[1]);
printf("Address : \t%u \tValue : \t%u\n", &a[2], a[2]);
printf("Address : \t%u \tValue : \t%u\n", &a[0][0], a[0][0]);
printf("Address : \t%u \tValue : \t%u\n", &a[1][0], a[1][0]);
printf("Address : \t%u \tValue : \t%u\n", &a[2][0], a[2][0]);

Address :       3669540         Value :         3669540
Address :       3669552         Value :         3669552
Address :       3669564         Value :         3669564
Address :       3669540         Value :         0
Address :       3669552         Value :         0
Address :       3669564         Value :         0


这里是数组a的内存布局:
Here''s the memory layout of your array a:
 a
 |
 v
 a[0]        a[1]        a[2]
 |           |           |
 v           v           v
[0] [1] [2] [3] [4] [5] [6] [7] [8]
 |   |   |   |   |   |   |   |   |
a00 a01 a02 a10 a11 a12 a20 a21 a22


a&a[0][0]一样指向数组的元素0,因此对于任何一个表达式您都得到相同的值.

令人困惑的是,虽然在语法上将a[0]a[1]a[2]视为持有指针的(常量)变量,但没有实际的内存持有这些指针变量的值.取而代之的是,它们保存的值是从变量a中存储的地址派生的.


a points to element 0 of your array, as does a[0], as does &a[0][0], so you get the same value for either expression.

What is confusing you is that while a[0], a[1] and a[2] are syntactically treated as (constant) variables holding pointers, there is no actual memory holding the value of these pointer variables. Instead the values they hold are derived from the address stored in the variable a.


这篇关于内存中2D阵列的阻碍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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