c - 非 int 类型的位字段?

标签 c visual-c++ bit-fields

我有一个使用如下声明的位域的代码

typedef struct my{
    const char *name;
    uint8_t is_alpha : 1;   
    uint8_t is_hwaccel : 1; 
    uint8_t x_chroma_shift; 
    uint8_t y_chroma_shift; 

} mystr; 

uint8_t 类型定义为 unsigned char

使用此位字段在 MS-VS 2008 中构建代码会出现如下警告:

imgconvert.c(60) : warning C4214: nonstandard extension used : bit-field types other than int.
  1. 使用非 int 类型的位字段是否有任何问题/潜在问题?为什么要发出警告?
  2. C99 C 语言规范是否允许除 int 类型的位文件外?

最佳答案

1] Is there any problems/potential issues in using bit fields of type other than int? Why the warning?

由于位字段是低级的,如果您使用非标准类型,则可能存在可移植性问题。因此出现了警告——请注意,它仍然是警告,而不是错误。

2] Are other than int type bit-fileds they allowed by C99 C language specification?

摘自C99草案:

6.7.2.1 Structure and union specifiers

4 A bit-field shall have a type that is a qualified or unqualified version of _Bool, signed int, unsigned int, or some other implementation-defined type.

关于c - 非 int 类型的位字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2280492/

相关文章:

无法将参数 'char (*)[30]' 转换为 'char*' 到 '1'

c++ - gcc 中的位域字节顺序

c - 位域可移植性

c++ - 对于大于 4GiB 的文件,fread 有时会返回错误值

c - 将字符串作为十六进制放入 Uint32_t

c - 解决底层管理功能UD的建议

c++ - 如何在不使用对象的情况下连接字符串、整数和 float ?

c++ - 为什么 Visual C++ 会在 delete[] 上覆盖这么多额外的内存?

c - 如何计算包含一些未命名成员和零大小位域的位域结构的大小?

c - PostgreSQL:给定的带占位符的类型与 PQexecPrepared() paramValues 参数中的类型之间的关系