您认为关于2D阵列的内存映射的讨论是正确的吗?特别是这张照片?你能解释一下这个理论吗?
假设我们在C中声明一个2D数组,如下所示:
int arr[3][3]={10, 20, 30, 40, 50, 60, 70, 80, 90};
Run Code Online (Sandbox Code Playgroud)
现在,根据这个讨论,内存将按如下方式排列:

现在,我编写了以下代码来测试这个理论:
#include <stdio.h>
main()
{
int arr[3][3]={10, 20, 30, 40, 50, 60, 70, 80, 90};
printf(" arr==%d\n", arr);
printf(" &arr[0]==%d\n", &arr[0]);
printf(" arr[0]==%d\n", arr[0]);
printf("&arr[0][0]=%d\n", &arr[0][0]);
printf(" arr[0][0]=%d\n", arr[0][0]);
}
/*
Output:
========
arr ==1245028
&arr[0] ==1245028
arr[0] ==1245028
&arr[0][0]==1245028
arr[0][0]==10
Press any key to continue...
*/
Run Code Online (Sandbox Code Playgroud)
为什么前4个输出相同?
请考虑以下代码
#include <stdio.h>
#define ROW_SIZE 2
#define COL_SIZE 2
int main()
{
int a[ROW_SIZE][COL_SIZE]={{1,2},{3,4}};
// Base address:Pointer to the first element a 1D array
printf("Base address of array:%p\n",a);
//The value at the base address: should be the address of 1st 1D array
printf("Value at the Base address:%p\n",*a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
获得的产出:
Sample Output:
Base address of array:0xbff77434
Value at the Base address:0xbff77434
Run Code Online (Sandbox Code Playgroud)
不知何故,我无法理解2D数组的基地址的概念和基地址的值,该地址是1D数组的地址相同.请解释.