struct命名为0 [英] struct named 0
问题描述
以下代码是否符合ANSI C?
typedef struct {
int a;
int b;
} doomdata;
int main(无效)
{
int x;
x =(int)&((doomdata *)0) - > b;
printf(" x =% d \ n",x);
返回x;
}
部分,
(int)&((doomdata *)0) - > b;
是否符合ANSI C?该怎么办?
感谢您的任何提示。
Napi
- -
http://www.axiomsol.com
http://www.cs.indiana.edu/hyplan/napi.html
推荐答案
Mohd Hanafiah Abdullah写道:
Mohd Hanafiah Abdullah wrote:
以下代码是否符合ANSI C?
typedef struct {
int a;
int b;
} doomdata;
int main(void)
{
int x;
x =(int)&((doomdata *)0) - > b;
printf(" x =%d \ n" ,x);
返回x;
}
部分,
(int)&((doomdata *)0) - > b;
是否符合ANSI C?该怎么办?
Is the following code conformat to ANSI C?
typedef struct {
int a;
int b;
} doomdata;
int main(void)
{
int x;
x = (int)&((doomdata*)0)->b;
printf("x=%d\n", x);
return x;
}
The part,
(int)&((doomdata*)0)->b;
is it conformant to ANSI C? What is it supposed to do?
根据之前的讨论,它至少是阴暗的但是
可能不符合,这取决于是否地址
0是否被取消引用。
这是来自< stddef.h>的偏移量宏的版本:
offsetof(doomdata ,b)
给出struct doomsdata中b的字节偏移量,这意味着
如果我们有
struct doomdata d;
unsigned char * p =(unsigned char *)& d;
然后db的地址是p + offsetof(doomdata,b)。
与offsetof扩展为表达式为
类型size_t相反,上面将给你一个int。
使用offsetof。
干杯
Michael
-
电子邮件:我的是/ at / gmx / dot / de地址。
According to prior discussions, it is at least shady but
probably not conformant, depending on whether the address
0 is dereferenced or not.
This is a version of the offsetof macro from <stddef.h>:
offsetof(doomdata,b)
gives you the byte offset of b in struct doomsdata, that means
if we have
struct doomdata d;
unsigned char *p = (unsigned char *)&d;
then the address of d.b is p+offsetof(doomdata,b).
In contrast to offsetof which expands to an expression of
type size_t, the above will give you an int.
Use offsetof.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Mohd Hanafiah Abdullah< na ** @ cs.indiana.edu>写道:
Mohd Hanafiah Abdullah <na**@cs.indiana.edu> wrote:
typedef struct {
int a;
int b;
} doomdata;
(int)&((doomdata *)0) - > b;
是否符合ANSI C?
typedef struct {
int a;
int b;
} doomdata; (int)&((doomdata*)0)->b; is it conformant to ANSI C?
IMO不,因为它没有指向任何有效的对象,所以
调用UB。
对他人的问题:
这是正确的吗?
(int)&((doomdata * )0) - > a;
-
Stan Tobias
mailx`echo si *** @FamOuS.BedBuG.pAlS.INVA LID | sed s / [[:upper:]] // g`
IMO no, because it doesn''t point to any valid object, so
invokes UB.
Question to others:
Would this be correct?
(int)&((doomdata*)0)->a;
--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
" Mohd Hanafiah Abdullah" < NA ** @ cs.indiana.edu>写了
"Mohd Hanafiah Abdullah" <na**@cs.indiana.edu> wrote
部分,
(int)&((doomdata *)0) - > b;
是否符合ANSI C?它应该做什么?
The part,
(int)&((doomdata*)0)->b;
is it conformant to ANSI C? What is it supposed to do?
这是offsetof()宏,它计算结构
成员相对于结构原点的偏移量。 br />
在大多数平台上都可以使用,但在不寻常的
系统上遇到了一些问题。例如,如果NULL不是所有位为零,或者如果存在陷阱
指针表示,则该方法不会自然地工作。所以我相信
在C89中它不需要工作。
This is the offsetof() macro, which calculates the offset of a structure
member relative to the struct origin.
On most platforms it works, but it suffers a few problems on unusual
systems. For instance, if NULL is not all bits zero, or if there are trap
pointer representations, then the method won''t work naturally. So I believe
that in C89 it is not required to work.
这篇关于struct命名为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!