[英]C - Forward declaration for struct and function
我试图弄清楚正向声明之间是如何相互作用的。 当正向声明采用带typedef结构的函数时,是否有办法让编译器接受先前正向声明(但未实际定义)的结构作为参数?
我正在工作的代码:
typedef struct{
int year;
char make[STR_SIZE];
char model[STR_SIZE];
char color[STR_SIZE];
float engineSize;
}automobileType;
void printCarDeets(automobileType *);
我希望我能做的是:
struct automobileType;
void printCarDeets(automobileType *);
//Defining both the struct (with typedef) and the function later
我觉得我要么缺少真正的基础知识,要么不了解编译器如何处理结构的前向声明。
Typedef和结构名称位于不同的命名空间中。 因此struct automobileType
和automobileType
不是同一件事。
为此,您需要给匿名结构一个标签名。
.c文件中的定义:
typedef struct automobileType{
int year;
char make[STR_SIZE];
char model[STR_SIZE];
char color[STR_SIZE];
float engineSize;
}automobileType;
头文件中的声明:
typedef struct automobileType automobileType;
void printCarDeets(automobileType *);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.