2015-01-17 27 views
0

我有定義的以下結構 - 座標的一個結構本身是一個父結構的成員編譯器警告傳遞陣列(本身結構的部件)到功能

typedef struct _coord { 
    double x; // time axis - seconds rather than samples 
    double y; 
} t_coord; 

typedef struct _algosc {      
    t_coord coords[COORD_COUNT];   
    //... struct continues beyond this but... 
} t_algosc; 

創建一個指針父結構然後分配內存。 object_alloc是特定於其他地方定義的API(MAX5)的malloc類型函數。這是所有工作,所以我不包括細節。

static t_class *algosc_class; // pointer to the class of this object 

    t_algosc *x = object_alloc(algosc_class) 

這是該函數的聲明,而我希望傳遞座標結構的陣列

void au_update_coords(t_coord (*coord)[]) 

餘數組傳遞給該函數如下,

au_update_coords(x->coords); 

它一切正常,但我得到編譯器警告

1>db.algosc~.c(363): warning C4047: 'function' : 't_coord (*)[]' differs in levels of indirection from 't_coord [4]' 
1>db.algosc~.c(363): warning C4024: 'au_update_coords' : different types for formal and actual parameter 1 

我無法解決傳遞結構的正確方法。任何人都可以幫忙也只是爲了我的薰陶,我會冒什麼樣的風險讓它保持原樣?

+1

您的示例聲明需要七個參數,但您只需使用一個參數調用該函數。我認爲這是通過不完整的傳遞來爲您的問題刪除不必要的代碼?請糾正它。 – ruakh

+1

數組自然衰減到指針,所以不需要傳遞指向數組的指針,只需傳遞數組本身(現在就是這樣),這當然必須在函數參數聲明中反映出來(即放棄指針聲明)。 –

+0

謝謝ruakh - 現在正在更正。 – DanBennett

回答

0

您需要將指針傳遞給數組,所以你需要把你的數組的地址,使之成爲指向數組的指針,這

au_update_coords(x->coords, otherArguments ...); 

應該成爲

au_update_coords(&x->coords, otherArguments ...); 

但你並不需要那個。如果擔心的功能不改變停在原地擔心它會數組,你需要的函數簽名從

void au_update_coords(t_coord (*coord)[], otherArguments ...) 

改變

void au_update_coords(t_coord *coord, otherArguments ...) 

和陣列直接通過在

au_update_coords(x->coords, otherArguments ...); 

當然,您可能需要修復au_update_coords()函數,無論您訪問陣列。

+0

,很好的回答。 – DanBennett

+1

'x-> coords'不是一個可變長度的數組,它有固定的維數'COORD_COUNT'。(結構不能包含VLA) –

+0

@iharob不,它不是;指向VLA的指針看起來像'T(* ptr)[n]'。這與指向不完整數組類型的指針「T(* ptr)[]」不同。 –