看板 C_and_CPP 關於我們 聯絡資訊
※ 引述《lueichun (= =)》之銘言: : 問題(Question): : 假設有一個陣列,叫做int a[5]; : 則a==&a==&a[0],a==&a[0]可以理解,但為何a==&a? : a的值是陣列開頭的位置(假設是1000),且a本身也佔有記憶體位置(假設是2000), : 那&a不是應該是那個記憶體空間位置(2000)嗎? : 所以應該是兩個不同的位置,但為何程式跑出來a==&a: : int main (int argc,char *argv[]){ : int a[5]; : printf("%p %p %p",a,&a[0],&a); : //結果為000000000062FE00 000000000062FE00 000000000062FE00 : return 0; : } : 二維陣列也是一樣的狀況,若有一陣列叫做num[3][4],則num==&num : num[0]==&num[0] num[1]==&num[1]...... : 我看書上都沒有解釋的很清楚,希望有人可以解惑,拜託了。 版上許多神人大大已提供許多觀點可供釐清, 小弟我僅補充 K&R 的書籍內容以供參詳。 K&R p83(chapter 5.1 pointer and Addresses) "...The & operator only applies to objects in memory: variables and array elements. It cannot be applied to expressions, constants, or register variables." K&R p89(chapter 5.3 Pointers and Arrays) "...But an array name is not a variable;..." 另外,根據 ISO/IEC 9899 對 object 的定義如下: region of data storage in the execution environment, the contents of which can represent values. 希望上述補充能令你質疑自己的想法(質疑使科學進步): 『且a本身也佔有記憶體位置』 p.s. 話說 K&R 沒有使用過 &a, 僅有 &a[0] 或 a 來表示第零個元素的位址, 蠻有趣的。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.224.137.104 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1623129496.A.74A.html
Lipraxde: 我記得 &a 好像是語法糖? 06/08 14:30