如何从位域转换为指针?
我编写了以下代码来生成
警告:初始化使指针来自整数而不进行强制转换
或 A
警告:从不同大小的整数转换为指针
警告:从 gcc (GCC) 4.1.1 20070105 (Red Hat 4.1.1-52)
struct my_t {
unsigned int a : 1;
unsigned int b : 1;
};
struct my_t mine = {
.a = 1,
.b = 0
};
const void * bools[] = { "ItemA", mine->a, "ItemB", mine->b, 0, 0 };
int i;
for (i = 0; bools[i] != NULL; i += 2)
fprintf(stderr, "%s = %d\n", bools[i], (unsigned int) bools[i + 1] ? "true" : "false");
如何让警告消失?无论我尝试什么投射,似乎总是会出现警告。
谢谢, 陈兹
I've written the following bit of code that is producing a
warning: initialization makes pointer from integer without a cast
OR A
warning: cast to pointer from integer of different size
from gcc (GCC) 4.1.1 20070105 (Red Hat 4.1.1-52)
struct my_t {
unsigned int a : 1;
unsigned int b : 1;
};
struct my_t mine = {
.a = 1,
.b = 0
};
const void * bools[] = { "ItemA", mine->a, "ItemB", mine->b, 0, 0 };
int i;
for (i = 0; bools[i] != NULL; i += 2)
fprintf(stderr, "%s = %d\n", bools[i], (unsigned int) bools[i + 1] ? "true" : "false");
How do I get the warning to go away? No matter what I've tried casting, a warning seems to always appears.
Thanks,
Chenz
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
嗯,为什么你坚持使用指针作为布尔值?这个替代方案怎么样?
Hmm, why do you insist on using pointers as booleans? How about this alternative?
此代码片段有几个问题:
mine
未声明为指针类型(至少在您发布的代码中没有),因此您不应使用->< /code> 组件选择运算符;
.
选择运算符,您将尝试将布尔值作为指针存储在 a 或 b 中,这不是您想要的;如果您尝试将布尔值与另一个对象关联,那么最好声明一个类型
并初始化一个数组,因为
位字段有其用途,但这不是其中之一。在这种情况下,您实际上并没有节省任何空间,因为需要分配至少一个完整的可寻址存储单元(字节、字等)来保存两个位字段。
There are several problems with this snippet:
mine
isn't declared as a pointer type (at least not in the code you posted), so you shouldn't be using the->
component selection operator;.
selection operator, you'd be attempting to store the boolean value in a or b as a pointer, which isn't what you want;If you're trying to associate a boolean value with another object, you'd be better off declaring a type like
and initialize an array as
Bit-fields have their uses, but this isn't one of them. You're really not saving any space in this case, since at least one complete addressable unit of storage (byte, word, whatever) needs to be allocated to hold the two bitfields.
两个问题:
不确定为什么要尝试将
unsigned int a:1
转换为void*
。如果您尝试引用它,语法将是&mine->a
而不是mine->a
,但是...您可以不创建一个指向 C 中的位的指针(至少据我所知)。如果您尝试创建指向位的指针,您可能需要考虑以下选项之一:
创建一个指向位域结构的指针(即
struct my_t *
),并(如有必要)使用单独的数字来指示要使用的位。示例:不要使用位字段。对每个标志使用
char
,因为它是您可以创建指向的指针的最小数据类型。确实使用位字段,但通过布尔运算手动实现它。示例:
Two problems:
Not sure why you are trying to convert
unsigned int a:1
to avoid*
. If you are trying to reference it, the syntax would be&mine->a
rather thanmine->a
, but...You can't create a pointer to a bit in C (at least as far as I know). If you're trying to create a pointer to a bit, you may want to consider one the following options:
Create a pointer to the bitfield structure (i.e.
struct my_t *
), and (if necessary) use a separate number to indicate which bit to use. Example:Don't use a bit field. Use
char
for each flag, as it is the smallest data type that you can create a pointer to.Do use a bit field, but implement it manually with boolean operations. Example:
有几种方法可以消除警告,但仍然使用指针值作为布尔值。例如,您可以这样做:
这使用
NULL
指针表示 false,并使用一个非空指针(在本例中为&bools
,但指向任何正确存储持续时间的对象就可以了)为 true。然后,您还可以在测试中删除对unsigned int
的强制转换,以便它只是:(空指针始终计算为 false,非空指针始终计算为 true)。
但是,我确实同意您最好创建一个
结构
数组。There's a few ways you could get rid of the warning, but still use a pointer value as a boolean. For example, you could do this:
This uses a
NULL
pointer for false, and a non-null pointer (in this case,&bools
, but a pointer to any object of the right storage duration would be fine) for true. You would also then remove the cast tounsigned int
in the test, so that it is just:(A null pointer always evaluates as false, and a non-null pointer as true).
However, I do agree that you are better off creating an array of
structs
instead.