分配位域结构 [英] assigning bit-field structure
问题描述
/ *使用以下内容:* /
typedef struct
{
unsigned char a:1;
unsigned char b:1;
} sss_t;
sss_t sss;
unsigned char ppp;
main()
{
sss =(sss_t)ppp; / *为什么''无效的演员表达''? * /
sss = *(sss_t *)& ppp; / *为什么好吗? * /
}
/ *
二手Sun编译器。
谢谢,
基斯
* /
/* With the following: */
typedef struct
{
unsigned char a : 1;
unsigned char b : 1;
} sss_t;
sss_t sss;
unsigned char ppp;
main()
{
sss = (sss_t)ppp; /* why ''invalid cast expression''? */
sss = *(sss_t*)&ppp; /* why OK? */
}
/*
Used Sun compiler.
Thanks,
Kees
*/
推荐答案
- Kees van der Bent - 写道:
- Kees van der Bent - wrote:
/ *使用以下内容:* /
typedef struct
{
unsigned char a:1;
unsigned char b:1;
} sss_t;
sss_t sss;
unsigned char ppp;
main()
{
sss =(sss_t)ppp; / *为什么''无效的演员表达''? * /
您正在尝试将无符号字符转换为用户定义的数据
类型,就编译器而言,现在是相关的。
sss = *(sss_t *)& ppp; / *为什么好吗? * /
/* With the following: */
typedef struct
{
unsigned char a : 1;
unsigned char b : 1;
} sss_t;
sss_t sss;
unsigned char ppp;
main()
{
sss = (sss_t)ppp; /* why ''invalid cast expression''? */
You are trying to convert an unsigned char to an user-defined data
type, that are noway related , as far as the compiler is concerned.
sss = *(sss_t*)&ppp; /* why OK? */
& ppp将其更改为UCHAR *,然后转换为(sss_t
*)[它确实变得危险在这里,但是你被允许这样做,因为
毕竟它们是地址。
转换后,你将它取消引用它。我假设你
写这个只是为了学习目的。但就这样,这样的演员表达式(将给定类型的指针更改为差异类型)应该不会在任何严肃的项目中使用
;)。 />
HTH。
&ppp changes it to UCHAR * , and then you are converting to (sss_t
*) [ it really gets dangerous here, but you are allowed to do it, since
they are addresses after all ] .
Having converted it, you are dereferencing it back. I am assuming you
are writing this for learning purposes only. But as such , such cast
expressions ( changing pointers of a given type to a diff. type ) ought
not to be used in any serious project ;) .
HTH .
" - Kees van der Bent - " < kv*@mbalance.moc>在消息中写道
news:40 ********************* @ news.xs4all.nl ...
"- Kees van der Bent -" <kv*@mbalance.moc> wrote in message
news:40*********************@news.xs4all.nl...
/ *使用以下内容:* /
typedef struct
{
unsigned char a:1;
unsigned char b:1;
} sss_t;
sss_t sss;
unsigned char ppp;
为什么这些全球?
main()
{
sss =(sss_t)ppp; / *为什么''无效的演员表达''? * /
你不会喜欢它,但你不能这样做,因为......你不能这样做!
您不能在标量和聚合类型之间进行转换。想想这个
方式:聚合(例如结构)是一个包含超过
一个值的对象(即使结构只包含一个字段,我们也是在这里谈论
一般条款)。标量只包含一个值。你怎么把
从一个转换成另一个?它有什么意义?
sss = *(sss_t *)& ppp; / *为什么好吗? * /
允许指针之间的转换。这种转换的结果是否有意义,甚至是一个有效的指针,都是另一个问题。
}
/ *
使用过的Sun编译器。
无所谓。
谢谢,
基斯
* /
/* With the following: */
typedef struct
{
unsigned char a : 1;
unsigned char b : 1;
} sss_t;
sss_t sss;
unsigned char ppp;
Why are these global?
main()
{
sss = (sss_t)ppp; /* why ''invalid cast expression''? */
You''re not gonna like it, but you can''t do it, because... you can''t do it!
You cannot cast between a scalar and an aggregate type. Think of it this
way: an aggregate (such as a structure) is an object containing more than
one value (even if the structure contains only one field, we are talking in
general terms here). A scalar only contains one value. How would you convert
from one to another? What sense would it make?
sss = *(sss_t*)&ppp; /* why OK? */
Conversions between pointers are allowed. Whether the result of such
conversion is meaningful, or even a valid pointer, is quite another
question.
}
/*
Used Sun compiler.
Doesn''t matter.
Thanks,
Kees
*/
>
Peter
Peter
Rakesh< dr ****************** @ yahoo。 COM>这样说:
Rakesh <dr******************@yahoo.com> spoke thus:
& ppp将其更改为UCHAR *,然后转换为(sss_t
&ppp changes it to UCHAR * , and then you are converting to (sss_t
^^^^^
ITYM" unsigned char" ;.UCHAR不是C关键字,当然,如果您愿意,可以自行输入。
< br $>
-
Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我在网络空间中获得
ataru(at) .org |不,我需要知道。火焰欢迎。
^^^^^
ITYM "unsigned char". UCHAR is not a C keyword, although of course
you can typedef it yourself if you''re so inclined.
--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.
这篇关于分配位域结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!