碰到以下面试题,不知怎样回答, 请教高手,Thanks in advance Describe a use for the following macro: #define X(y, z) ((int) &(((y *) 0)- >z))
T*i
2 楼
月经题 to get the offset of the member z within a struct/class y
)-
【在 b***m 的大作中提到】 : 碰到以下面试题,不知怎样回答, 请教高手,Thanks in advance : Describe a use for the following macro: #define X(y, z) ((int) &(((y *) 0)- : >z))
P*e
3 楼
这个y *是什么意思
【在 T*******i 的大作中提到】 : 月经题 : to get the offset of the member z within a struct/class y : : )-
c*t
4 楼
weak, pointer cast.
【在 P********e 的大作中提到】 : 这个y *是什么意思
T*i
5 楼
ft,你也是个老警察了吧
【在 P********e 的大作中提到】 : 这个y *是什么意思
g*s
6 楼
trick在((y*) 0),不在(y*)本身吧。 class A{ }; A *x=0; A *y=(A*) 0; Here x!=y? 月经对初潮的也是新鲜事,哈哈。
【在 T*******i 的大作中提到】 : ft,你也是个老警察了吧
M*m
7 楼
do you mind explaining what does that mean to assign 0 to a pointer? does that mean it will point to memory location 0?
【在 g*********s 的大作中提到】 : trick在((y*) 0),不在(y*)本身吧。 : class A{ : }; : A *x=0; : A *y=(A*) 0; : Here x!=y? : 月经对初潮的也是新鲜事,哈哈。
t*t
8 楼
Of course x==y. what are you talking about!
【在 g*********s 的大作中提到】 : trick在((y*) 0),不在(y*)本身吧。 : class A{ : }; : A *x=0; : A *y=(A*) 0; : Here x!=y? : 月经对初潮的也是新鲜事,哈哈。
t*t
9 楼
0 cast to any pointer type returns null pointer. null pointer is null pointer, it doesn't point to anything.
【在 M*m 的大作中提到】 : do you mind explaining what does that mean to assign 0 to a pointer? does : that mean it will point to memory location 0?
M*m
10 楼
I am confused by (A *)0 -> B, #include #define X(y, z)((int)&(((y*)0)->z)) #define X1(y, z)((int)&(((y*)1)->z)) #define X2(y, z)((int)&(((y*)2)->z)) struct A{ int AA; int AB; int AC; }; int main(){ printf("%d %d %d\n", X(A,AC), X1(A,AC), X2(A,AC)); } the result is 8, 9, 10.
【在 t****t 的大作中提到】 : 0 cast to any pointer type returns null pointer. null pointer is null : pointer, it doesn't point to anything.
It's the memory address. I am wondering what happened when we replace 0 by 1 and 2. // very weak. 0 is cast to NULL, what about 1 and 2?
【在 T*******i 的大作中提到】 : 啥叫绝对地址?
T*i
16 楼
那不就结了。 一和二呀。null就是零呀。
【在 M*m 的大作中提到】 : It's the memory address. : I am wondering what happened when we replace 0 by 1 and 2. // very weak. : 0 is cast to NULL, what about 1 and 2?
M*m
17 楼
Hmm, still confusing... what does NULL mean exactly? as most book says a pointer is a variables that stores the address. so if we say struct A* p=0, that means variable p stores value 0, therefore points to memory address 0. If this is true, it same reasonable to get 9 when we store 1 to p. As it seems to me that NULL pointer points to no where. But when we cast 1 or 2, p would point to somewhere in memory - address 1 or 2, is that correct?
【在 T*******i 的大作中提到】 : 那不就结了。 : 一和二呀。null就是零呀。
T*i
18 楼
重复一遍,null就是零。 查看cstddef (in c++)/stddef.h (in c)
that store correct?
【在 M*m 的大作中提到】 : Hmm, still confusing... : what does NULL mean exactly? as most book says a pointer is a variables that : stores the address. so if we say struct A* p=0, that means variable p : stores value 0, therefore points to : memory address 0. If this is true, it same reasonable to get 9 when we store : 1 to p. : As it seems to me that NULL pointer points to no where. But when we cast 1 : or 2, p would point to somewhere in memory - address 1 or 2, is that correct?
actually, in c++, you can't do that, since void* can't be casted to T*. so in c++, it's simply #define NULL 0 in fact, this is what linux do: #if defined(__cplusplus) #define NULL 0 #else #define NULL ((void *)0) #endif
current c++ is a bit messy in this point. i heard c++0x is proposing a new keyword "nullptr" for null pointer and pointer type.
is
【在 g*********s 的大作中提到】 : Wherever NULL appears, it refers a pointer typed symbol, no matter how it is : implemented. Actually, I prefer "const void* NULL(0);", if possible.