![](/img/trans.png)
[英]passing an argument ( a struct in an array of structs within a struct) to a function in C
[英]Compiler Warning Passing array of structs (itself member of struct) to Function
我定義了以下結構-坐標結構本身就是父結構的成員
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
我無法解決傳遞該結構的正確方法。 誰能幫忙。 同樣出於我的興趣,我冒着什么樣的風險冒着它原樣?
您需要傳遞一個指向數組的指針,因此您需要獲取數組的地址以使其成為指向數組的指針,這
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()
函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.