![](/img/trans.png)
[英]In C, is the address of the first element in an array the same as the address of the array?
[英]How can the compiler in C get the difference between the address of the first element of the array and the address of the whole array?
鑒於這個問題,我完全了解以下事實:
1) 數組的名稱是指向它的第一個元素的固定指針。
2) 二維矩陣的名稱是它的第一個一維數組的地址。
3) 以及 x[i] 和 &x[i] 在算術運算中的區別(其中 i 是非負整數)。
編譯器是MinGW C中的編譯器如何獲得數組第一個元素的地址和整個數組的地址之間的差異,盡管它們持有相同的數值?
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main(void)
{
int x[3][3] = {1,2,3,4,5,6,7,8,9} ;
int *p ;
for(p = &x[0] ; p < &x[0] + 1 ; p++) //Warning: comparison of distinct pointer types in this line of code
printf("%d\n",*p) ;
return 0 ;
}
您正在分配/比較兩種不同的指針類型。
x
具有式int [3][3]
即,尺寸3的陣列的尺寸3的陣列int
。 從那里, x[0]
具有類型int [3]
,即int
大小為 3 的數組。
隨后, &x[0]
類型為int (*)[3]
,即指向int
大小為 3 的數組的指針。 相反, p
類型為int *
,即指向int
指針。 這些是不同的類型,這就是編譯器向您發出警告的原因。
編譯器或多或少以我描述的方式解析表達式,查看對象的類型並查看結果表達式的類型如何隨着每個運算符的應用而變化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.