2017-08-11 117 views
1

我有代碼:爲什麼我的代碼給出「結構返回值與函數類型不匹配」編譯錯誤?

typedef struct t 
{ 
    uint8 a[100]; 

}t; 

t tt; //object of the struct 
f(&tt); //some file calling the func 

//function body in some file 
uint8 *f(const struct t *ptr) 
{ 
    return ptr->a; 
} 

當我嘗試建立我的錯誤:

Return value type does not match the function type.

我缺少的東西?

+0

你肯定包含包含函數原型的頭文件和看得見的每一個翻譯單元? –

+4

試'的typedef struct' - >'typedef結構t' – BLUEPIXY

+2

我會懷疑'const'造成麻煩。錯誤消息是否顯示更多細節? – Gerhardh

回答

1

您需要使用類型的名字,還有你的代碼的任何地方沒有定義struct t型,所以

uint8 *f(t *const tt); 

應該是函數簽名,當然,我想你在使用meaninful名字你真實的代碼。

另外請注意,我並沒有使指針對象const因爲如果返回一個非const指針指向一個const指針結構,那麼不確定的行爲可能發生的其它替換過程

const uint8 *f(const t *const tt); 

的第二個const,只是防止意外重新分配tt

+0

UINT8 * F(常量結構T * PTR)是從狂想曲生成的函數原型。這是沒有問題的 –

+0

@AkshayImmanuelD然後,'結構T {/ *這裏定義* /};'是你所需要的,沒有'typedef'。 –

+0

我認爲問題是我不能返回一個非const指針到const struct成員。如果我將函數原型設置爲struct t * ptr,代碼就會生成。 –

相關問題