C 中不兼容的枚举类型
我有一个关于枚举和数组的问题。本质上,我有一个枚举“BIT”数组,声明为枚举类型“word”。
typedef enum {
ZERO = (uint8_t) 0, ONE = (uint8_t) 1
} BIT;
typedef BIT word[16];
正如向我解释的那样,“单词”只是一个预定义的 16 位数组。但是,当我尝试分配给已声明的单词时,我只是收到一条错误消息,指出类型单词和位不兼容。
BIT ten = ZERO;
word Bob;
Bob[10] = ten;
我只能用另一个单词写入单词 Bob 吗?我会想,因为“单词”是一个“BIT”数组,所以我可以简单地将一个位分配给“单词”数组中的一个位置。
I have a question about enums and arrays. Essentially I have an array of enum "BIT"s declared as an enum type "word".
typedef enum {
ZERO = (uint8_t) 0, ONE = (uint8_t) 1
} BIT;
typedef BIT word[16];
As it was explained to me, "word" is simply a predefined array of 16 BITs. However, when I try to assign to a declared word, I simply get an error saying incompatible type word and BIT.
BIT ten = ZERO;
word Bob;
Bob[10] = ten;
Can I only write to the word Bob with another word, I would have thought since "word" is an array of "BIT"s that I could simply assign a bit to a position in the "word" array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
上下文
问题的一个版本包含代码:
问题说明问题
在于
word_not()
的参数是指向数组的指针。内部的符号必须相应调整:尽管您可以更简洁地写为:
或者,您可以简单地将函数重新定义为:
或者,再次更简洁地,为:
可编译测试用例 - 输出
可编译测试用例 - 源
Context
One version of the question included the code:
Explanation of the problem
The problem is that arguments to
word_not()
are pointers to arrays. The notation inside has to be adjusted accordingly:Though you could write that more succinctly as:
Alternatively, you can simply redefine the function as:
Or, again, more succinctly, as:
Compilable test case - output
Compilable test case - source
我认为问题在于您试图在函数范围之外的文件范围内执行
Bob[10] = ten;
赋值。你不能那样做。在文件范围内,您无法自由索引内容,也无法使用常量值以外的任何内容初始化变量,十不是其中之一。现在,我有点模糊为什么下面的代码不想编译(使用 gcc 3.4.4 和 Open Watcom 1.9):
两个编译器都说 Bob[10] 和 Bob2[10] 没有用常量初始化。
I think the problem is that you're trying to do the
Bob[10] = ten;
assignment outside of function scope, at the file scope. You can't do that. At the file scope you can't index things freely and you can't initialize variables with anything other than constant values, ten isn't one of them.Now, I'm a little fuzzy as to why the below doesn't want to compile (using gcc 3.4.4 and Open Watcom 1.9):
Both compilers say Bob[10] and Bob2[10] aren't being initialized with constants.