我是C编程的初学者,但我想知道typedef在定义结构时使用与使用结构之间有什么区别typedef.在我看来,实际上没有区别,他们实现了同样的目标.
struct myStruct{
int one;
int two;
};
Run Code Online (Sandbox Code Playgroud)
与
typedef struct{
int one;
int two;
}myStruct;
Run Code Online (Sandbox Code Playgroud) 我想要更好地理解C并且在试图理解我自己得到的错误时,我遇到了这个问题.我最终明白我做错了什么,但那个问题的代码让我感到困惑:
struct {
uint8_t time;
uint8_t type;
uint8_t phase;
uint8_t status;
} Raw_data_struct;
typedef struct Raw_data_struct Getst_struct;
void Getst_resp(Getst_struct Data);
Run Code Online (Sandbox Code Playgroud)
据我所知,问题是结构的名称放在了错误的位置,这意味着结构是匿名定义的,因此当使用typedef时,名称"Raw_data_struct"不可用.
但是,编译器对名称做了什么?我在这上面检查了cpp引用站点,但是它们只提到了两种类型的struct声明,其中一种是定义.在struct-declaration-list之后,该定义似乎没有任何允许.然而,我没有尝试过的编译器确定这是一个错误(我尝试了gcc和clang).
我想了解如何使用struct声明.名称"Raw_data_struct"是用于任何事情,还是被忽略?这有什么原因不是错误吗?
感谢您阅读本文!