编译器警告将结构数组(结构本身的成员)传递给函数

标签 c arrays pointers struct

我定义了以下结构 - 坐标结构本身就是父结构的成员

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/

相关文章:

python - 将多个数据帧列连接到数据帧列下的单个数组中

javascript - 使用数组和 Javascript 进行有限的复选框选择

java - 空指针异常

当我插入变量时 C 程序崩溃

c++ - 使用 Clang 编译 DLL 时指定 DEF 文件

c - 在不排序的情况下删除数组中的重复元素 - 错误输出

c - 将指向结构体的指针数组传递给函数

c - 从叶子开始逐级修剪二叉流程树

c - 如何实现我的后缀算法的数学POW - C

c - C 中的指针和地址