我定义了以下结构 - 坐标结构本身就是父结构的成员
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()
函数。
关于编译器警告将结构数组(结构本身的成员)传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28002302/