将结构转换为int [英] Casting a structure to an int

查看:92
本文介绍了将结构转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了以下CAST宏,它可以向任何其他东西投射任何



#define CAST(new_type,old_object)(* ((new_type *)& old_object))


union

{

char ch [4];

int i [2];

} my_union;


long longvar;


longvar = (长)my_union;非法演员

////////////////非法

longvar = CAST(长,my_union);合法演员


我理解为什么CAST宏有效,但我无法理解

以下演员的问题是什么:


longvar =(长)my_union;


有人可以告诉我上面的

投射机制有什么问题?


PS这不是作业。我从以下

链接中获取此代码:
http://paul.rutgers.edu/~rhoads/Code/cast_anything.c

I recently came across the following CAST macro which can cast anything
to any other thing:

#define CAST(new_type,old_object) (*((new_type *)&old_object))

union
{
char ch[4];
int i[2];
} my_union;

long longvar;

longvar = (long)my_union; Illegal cast
//////////////// ILLEGAL
longvar = CAST(long, my_union); Legal cast

I understands that why the CAST macro works but I cannot understands
that what is the problem in the following casting :

longvar = (long)my_union;

Can somebody please tell me that what is the problem in the above
casting mechanism?

PS This is not an assignment. I took this code from the following
link:
http://paul.rutgers.edu/~rhoads/Code/cast_anything.c

推荐答案

你好,


见插入的评论:

ca ******** @ yahoo.com 写道:
我最近遇到了以下CAST宏,它可以向任何其他东西施放任何东西:

#define CAST(new_type,old_object)(*((new_type *)& old_object))

联盟
{ch /> char ch [4] ;
int i [2];
} my_union;

long longvar;

longvar =(long)my_union;非法演员
//////////////// ILLEGAL


my_union的大小?和sizeof of long?

longvar = CAST(long,my_union);合法演员

我理解为什么CAST宏有效,但我无法理解下面的演员有什么问题:

longvar =(long)my_union ;

有人可以告诉我上面的演员机制有什么问题吗?

PS这不是作业。我从以下
链接中获取此代码:
http://paul.rutgers.edu/~rhoads/Code/cast_anything.c
I recently came across the following CAST macro which can cast anything
to any other thing:

#define CAST(new_type,old_object) (*((new_type *)&old_object))

union
{
char ch[4];
int i[2];
} my_union;

long longvar;

longvar = (long)my_union; Illegal cast
//////////////// ILLEGAL
Sizeof of my_union? and sizeof of long?
longvar = CAST(long, my_union); Legal cast

I understands that why the CAST macro works but I cannot understands
that what is the problem in the following casting :

longvar = (long)my_union;

Can somebody please tell me that what is the problem in the above
casting mechanism?

PS This is not an assignment. I took this code from the following
link:
http://paul.rutgers.edu/~rhoads/Code/cast_anything.c




抛出任何东西或任何东西的第一个字节甚至所有它和一些

部分谁知道什么。实用,美观,危险。


亲切的问候。



"cast anything or the first bytes of anything or even all it and some
parts of who knows what". Practical, nice and dangerous.

Kind regards.


ca ******** @ yahoo.com 写道:
我最近遇到了以下CAST宏,它可以投

#define CAST(new_type,old_object)(*((new_type *)& old_object))
I recently came across the following CAST macro which can cast anything
to any other thing:

#define CAST(new_type,old_object) (*((new_type *)&old_object))




一个不应该做的事情的完美例子。



A perfect example of what shouldn''t be done.


ca ******** @ yahoo.com 写道:
ca********@yahoo.com writes:
我理解为什么CAST宏工作但我无法理解
以下演员中的问题是什么:

longvar =(long)my_union;
I understands that why the CAST macro works but I cannot understands
that what is the problem in the following casting :

longvar = (long)my_union;




在演员表达式中,表达式的值必须使用标量类型,但是联合不是标量类型。 (标量

类型是算术类型或指针类型。)

-

我应该杀死你,你站在哪里,没有价值的人。 --Kaz



In a cast expression, the expression whose value is cast must
have a scalar type, but a union is not a scalar type. (A scalar
type is an arithmetic type or a pointer type.)
--
"I should killfile you where you stand, worthless human." --Kaz


这篇关于将结构转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆