[英]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.