Re: [問題] address operator &
很多基礎就不打了,只打一些重點
設環境 x86_32
char* a -> pointer to char
int* b -> pointer to int
int c[4] -> array of int
int (*d)[4] -> pointer to an array of int
sizeof(a) = sizeof(b) = 4
sizeof(char) = 1
sizeof(int) = 4
sizeof(c) = 4 * sizeof(int) = 16
sizeof(d) = sizeof(void*) = 4
//設 a = 0x0,b = 0x0 , &c=0x0
a++ 後為0x0 + sizeof(char) = 0x1
b++ 後為0x0 + sizeof(int) = 0x4
c 實質上是 the address of the first element of an array
&c 是 a pointer to [an array of int]
int *p = c 會被隱型轉成 int *p = &c[0]
int *p = &c 因為兩邊型態不同,所以被強制轉成 int*
c = &c = 0x0 只是型態不同
c 是 int *
&c 是 int (*)[4]
------------
所以回到原文
int*p = &A;
兩邊的型態一樣嗎?
Ans: 不一樣
p 是 a pointer to int
&A 是 a pointer to [an array of int]
在 &A = A 的情況下被強制隱性轉為 int*
這個特性要注意做指標運算時要注意接下來的 pointer type 是不是你要的
※ 引述《zzss2003 (brotherD)》之銘言:
: 網址:
: https://stackoverflow.com/questions/49890211/address-operator-and-array-in-assembly-level
: 在Answer裡面提到:
: The address of & operator allows you to construct a pointer to value from a
: variable of same type as value.
: 中文翻譯:address operator允許你建構一個指標(from一個變數to value,且這個變數有
: 跟value一樣的type)
: 請問,這一行的意思是不是指,當執行程式這一行時:
: p = &var1;
: &本身也會占用一個variable的空間(用來放var1的address),然後在把這個address給p?
: 接下來,內文提到:
: int *p = &A;
: 程式在做這行指令時,you are doing an assignment from a pointer to array of int
: into a variable of type array of int, which is a different type.
: 光看這行英文,哪有different type,一樣是array of int啊
: 還是他打錯了?應該是from a pointer to int into a variable of type array of int?
: 謝謝。
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.227.39.219
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1524404003.A.03A.html
※ 編輯: enonrick (36.227.39.219), 04/22/2018 21:34:06
※ 編輯: enonrick (36.227.39.219), 04/22/2018 21:36:17
推
04/23 09:30,
6年前
, 1F
04/23 09:30, 1F
推
04/23 11:53,
6年前
, 2F
04/23 11:53, 2F
推
04/23 12:04,
6年前
, 3F
04/23 12:04, 3F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):