2010-07-28 31 views
1

typedef void(callback)(int * p1,sStruct * p2);經常性聲明c

typedef struct _sStruct 
{ 
callback *funct; 
}sStruct; 

我有下面的聲明,在C.我怎樣才能編譯這個經常性聲明沒有收到任何錯誤?

目前我收到:語法錯誤在'*'令牌之前的第一行。

+0

什麼是「經常申報」?你的意思是循環聲明還是自引用? – progrmr 2010-07-28 17:39:54

回答

11

您可以前瞻性聲明結構:

/* Tell the compiler that there will be a struct called _sStruct */ 
struct _sStruct; 

/* Use the full name "struct _sStruct" instead of the typedef'ed name 
    "sStruct", since the typedef hasn't occurred yet */ 
typedef void (callback)(int *p1, struct _sStruct *p2); 

/* Now actually define and typedef the structure */ 
typedef struct _sStruct 
{ 
    callback *funct; 
} sStruct; 

編輯:更新以匹配類型名的問題的變化。

此外,我強烈建議您不要給該結構標識符_sStruct。以_開頭的全局名稱是保留名稱,將它們用於您自己的標識符可能導致未定義的行爲。

+1

您也可以一次性轉發聲明'typedef',就像'typedef struct sStruct sStruct;'一樣。並且不要猶豫,把結構和'typedef'同名。 – 2010-07-28 17:07:14