由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 一道面试题
相关主题
弱问c++里有没有NULL这个keyword?C++ Q05: pointer to constant variable
高手看看,这个define是用来干什么的?one more interview question
c ptr questionc++ question
C 多线程的一个问题One question about Void pointer
请教两道linux面试题目Questions about c code
请教大家一道C的面试题自学C语言-书和online resources (转载)
请问可以这样定义struct吗?Python Q: function pass in struct pointer, come back with data filled
一个简单的小问题问个c++ struct和指针问题
相关话题的讨论汇总
话题: null话题: pointer话题: define话题: int话题: ac
进入Programming版参与讨论
1 (共1页)
b***m
发帖数: 58
1
碰到以下面试题,不知怎样回答, 请教高手,Thanks in advance
Describe a use for the following macro: #define X(y, z) ((int) &(((y *) 0)-
>z))
T*******i
发帖数: 4992
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
发帖数: 2610
3
这个y *是什么意思

【在 T*******i 的大作中提到】
: 月经题
: to get the offset of the member z within a struct/class y
:
: )-

c*****t
发帖数: 1879
4
weak, pointer cast.

【在 P********e 的大作中提到】
: 这个y *是什么意思
T*******i
发帖数: 4992
5
ft,你也是个老警察了吧

【在 P********e 的大作中提到】
: 这个y *是什么意思
g*********s
发帖数: 1782
6
trick在((y*) 0),不在(y*)本身吧。
class A{
};
A *x=0;
A *y=(A*) 0;
Here x!=y?
月经对初潮的也是新鲜事,哈哈。

【在 T*******i 的大作中提到】
: ft,你也是个老警察了吧
M*m
发帖数: 141
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
发帖数: 6806
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
发帖数: 6806
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
发帖数: 141
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.

相关主题
请教大家一道C的面试题C++ Q05: pointer to constant variable
请问可以这样定义struct吗?one more interview question
一个简单的小问题c++ question
进入Programming版参与讨论
T*******i
发帖数: 4992
11
X(y, z)返回的是y struct/class中member z的绝对地址。
只有object指针指向0的时候才能得到相对地址即offset

【在 M*m 的大作中提到】
: 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;
: };

M*m
发帖数: 141
12
then why
#define X1(y, z)((int)&(((y*)1)->z))
returns 9?

【在 T*******i 的大作中提到】
: X(y, z)返回的是y struct/class中member z的绝对地址。
: 只有object指针指向0的时候才能得到相对地址即offset

k****f
发帖数: 3794
13
1+2*sizeof(int)=9

【在 M*m 的大作中提到】
: then why
: #define X1(y, z)((int)&(((y*)1)->z))
: returns 9?

T*******i
发帖数: 4992
14
啥叫绝对地址?

【在 M*m 的大作中提到】
: then why
: #define X1(y, z)((int)&(((y*)1)->z))
: returns 9?

M*m
发帖数: 141
15
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
发帖数: 4992
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
发帖数: 141
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
发帖数: 4992
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?

g*********s
发帖数: 1782
19
我想大多数人对NULL的理解就是NULL表示不指向任何有效对象。但即使不指向有效对象,依然可以进行->和&操作。这是个盲点。a->x就是*(a+offset(x)),&(a->x)就是a+offset(x)。a==0时,自然就是offset(x)。
不过这个“null就是0”的说法看怎么理解了。我也可以说不是一回事,因为类型不一样。我不认为(!p)是一种好的风格。

【在 T*******i 的大作中提到】
: 重复一遍,null就是零。
: 查看cstddef (in c++)/stddef.h (in c)
:
: that
: store
: correct?

p**********g
发帖数: 187
20
Weak! #define NULL ((void*) 0)
> 不过这个“null就是0”的说法看怎么理解了。我也可以说不是一回事,因为类型不
一样。我不认为(!p)是一种好的风格。
相关主题
One question about Void pointerPython Q: function pass in struct pointer, come back with data filled
Questions about c code问个c++ struct和指针问题
自学C语言-书和online resources (转载)最近学了一下 Go
进入Programming版参与讨论
t****t
发帖数: 6806
21
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

【在 p**********g 的大作中提到】
: Weak! #define NULL ((void*) 0)
: > 不过这个“null就是0”的说法看怎么理解了。我也可以说不是一回事,因为类型不
: 一样。我不认为(!p)是一种好的风格。

g*********s
发帖数: 1782
22
Wherever NULL appears, it refers a pointer typed symbol, no matter how it is
implemented. Actually, I prefer "const void* NULL(0);", if possible.

【在 p**********g 的大作中提到】
: Weak! #define NULL ((void*) 0)
: > 不过这个“null就是0”的说法看怎么理解了。我也可以说不是一回事,因为类型不
: 一样。我不认为(!p)是一种好的风格。

t****t
发帖数: 6806
23
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.

b*****e
发帖数: 474
24
不完全对. NULL确实不指向有效对象, 所以 *NULL 一定会出错.
但是这道题里, &(0->x) 并没有做 *0 操作, 直接变成 0 + offset(x),
应该是编译器干的.

象,依然可以进行->和&操作。这是个盲点。a->x就是*(a+offset(x)),&(a->x)就是a+
offset(x)。a==0时,自然就是offset(x)。
一样。我不认为(!p)是一种好的风格。

【在 g*********s 的大作中提到】
: 我想大多数人对NULL的理解就是NULL表示不指向任何有效对象。但即使不指向有效对象,依然可以进行->和&操作。这是个盲点。a->x就是*(a+offset(x)),&(a->x)就是a+offset(x)。a==0时,自然就是offset(x)。
: 不过这个“null就是0”的说法看怎么理解了。我也可以说不是一回事,因为类型不一样。我不认为(!p)是一种好的风格。

1 (共1页)
进入Programming版参与讨论
相关主题
问个c++ struct和指针问题请教两道linux面试题目
最近学了一下 Go请教大家一道C的面试题
问个土问题:什么是satellite data?请问可以这样定义struct吗?
问个时钟的问题一个简单的小问题
弱问c++里有没有NULL这个keyword?C++ Q05: pointer to constant variable
高手看看,这个define是用来干什么的?one more interview question
c ptr questionc++ question
C 多线程的一个问题One question about Void pointer
相关话题的讨论汇总
话题: null话题: pointer话题: define话题: int话题: ac