C语言 C: typedef 联合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14197998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C: typedef union
提问by dccharacter
didn't find anything in related questions. Most probably it's super noob, but I'll ask anyway/ I've got the following in my .h file:
在相关问题中没有找到任何内容。很可能是超级菜鸟,但我还是会问/我的 .h 文件中有以下内容:
typedef union _API_Packet_0x90{
uint8_t packet[26];
struct _pack_struct {
uint8_t start;
uint8_t length[2];
uint8_t addr64[8];
uint8_t addr16[2];
uint8_t options;
uint8_t rfData[4];
uint8_t chksum;
};
} API_Packet_0x90;
API_Packet_0x90 ap90;
This is code for a microcontroller, I'm using xc8 toolchain (former Hi Tech C). The compiler says:
这是微控制器的代码,我使用的是 xc8 工具链(前 Hi Tech C)。编译器说:
xbee_api.h:19: warning: missing basic type; int assumed
xbee_api.h:19: error: ";" expected
xbee_api.h:19: warning: missing basic type; int assumed
xbee_api.h:21: warning: missing basic type; int assumed
xbee_api.h:19: 警告:缺少基本类型;int 假定
xbee_api.h:19: 错误: ";" 预期
xbee_api.h:19: 警告: 缺少基本类型;int 假定
xbee_api.h:21: 警告:缺少基本类型;假设为整数
, and this goes on (too many errors)
,然后继续(错误太多)
I thought it's uint8_t, so I added #include <ctypes.h>. Nope. I thought it is about names, so I tried all kinds of plays such as
我以为是 uint8_t,所以我添加了#include <ctypes.h>. 不。我以为是关于名字的,所以我尝试了各种戏剧,例如
typedef union {
uint8_t packet[26];
struct _pack_struct {
};
} API_Packet_0x90;
or
或者
typedef union {
uint8_t packet[];
struct _pack_struct {
};
} API_Packet_0x90;
or others. Nothing helps. I'm stuck as I believe I'm following syntax properly. Any help?
或其他。没有任何帮助。我被卡住了,因为我相信我正确地遵循了语法。有什么帮助吗?
回答by Lundin
uint8_tis located in stdint.h, not in ctype.h(nor ctypes.h, no such header exists). You must use a compiler that follows a newer version of the C standard for this header to be found (C99 or C11 standards).
uint8_t位于stdint.h,不在ctype.h(也不在 ctypes.h,不存在这样的头文件)。您必须使用遵循更新版本的 C 标准的编译器才能找到此头文件(C99 或 C11 标准)。

