作者enonrick (EnonRick)
看板C_and_CPP
标题Re: [问题] address operator &
时间Sun Apr 22 21:33:21 2018
很多基础就不打了,只打一些重点
设环境 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://webptt.com/cn.aspx?n=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
1F:推 zzss2003: 精辟 04/23 09:30
2F:推 KanzakiHAria: 这篇其实有讲清楚了 原PO还是卡住XD 04/23 11:53
3F:推 splasky: 推 04/23 12:04