C 如何将指向结构的指针数组传递给函数?
考虑一个表示笛卡尔坐标中一个点的结构C 如何将指向结构的指针数组传递给函数?,c,pointers,struct,compound-literals,C,Pointers,Struct,Compound Literals,考虑一个表示笛卡尔坐标中一个点的结构 struct point { float x, y; }; typedef struct point point_t; 我有一个函数,它接受一组点,并根据传递的点绘制一条曲线,其定义如下所示 void beziercurve(int smoothness, size_t n, point_t** points) point_t **p={[0]=(point_t*){.x=1.0, .y=1.0}, [1]=(point_t*)
struct point { float x, y; };
typedef struct point point_t;
我有一个函数,它接受一组点,并根据传递的点绘制一条曲线,其定义如下所示
void beziercurve(int smoothness, size_t n, point_t** points)
point_t **p={[0]=(point_t*){.x=1.0, .y=1.0},
[1]=(point_t*){.x=2.0, .y=2.0},
[2]=(point_t*){.x=4.0, .y=4.0}};
beziercurve(100, 3, p);
我已经编写了贝塞尔函数bezier
,我想测试我的函数是否工作正常。因此,在主函数中,我通过复合文本将以下伪值传递给函数
point_t **p={(point_t*){.x=1.0, .y=1.0},
(point_t*){.x=2.0, .y=2.0},
(point_t*){.x=4.0, .y=4.0}};
beziercurve(100, 3, p);
LLVM给了我以下错误
bezier.c:54:44: error: designator in initializer for scalar type 'point_t *'
(aka 'struct point *')
point_t** p=(point_t**){(point_t*){.x=1.0,.y=1.0},(point_t*){.x=2.0,.y=2.0...
^~~~~~
我甚至试过这样的东西
void beziercurve(int smoothness, size_t n, point_t** points)
point_t **p={[0]=(point_t*){.x=1.0, .y=1.0},
[1]=(point_t*){.x=2.0, .y=2.0},
[2]=(point_t*){.x=4.0, .y=4.0}};
beziercurve(100, 3, p);
但这也不起作用。我的逻辑是这样的:(point_t*){.x=1.0,.y=1.0}
创建一个指向临时结构的指针,而这些结构指针在弯曲的括号内创建了一组指针数组,我可以传递给函数
我错过了什么?为什么代码不起作用?此复合文字不起作用:
(point_t*){.x=1.0, .y=1.0}
因为它试图说初始值设定项{.x=1.0,.y=1.0}
是指针,但它不是
要创建指向结构的指针数组,需要执行以下操作:
point_t *p[]={&(point_t){.x=1.0, .y=1.0},
&(point_t){.x=2.0, .y=2.0},
&(point_t){.x=4.0, .y=4.0}};
然而,我怀疑您实际上需要的只是一个结构数组。然后您可以这样创建它:
point_t p[] = {
{.x=1.0, .y=1.0},
{.x=2.0, .y=2.0},
{.x=4.0, .y=4.0}
};
然后将函数更改为将指针指向点\u t
:
void beziercurve(int smoothness, size_t n, point_t *points)
(point_t*){.x=1.0,.y=1.0}
创建指向临时结构的指针-指针没有字段,因此语法无效。您可以创建结构本身的复合文本,并传递它的地址,例如&(point_t){.x=1.0、.y=1.0}
@EugeneSh。你能把整个数组的初始化写下来吗?对我来说,point_t**p={&(point_t){.x=1.0,.y=1.0},&(point_t){.x=2.0,.y=2.0},&(point_t){.x=4.0,.y=4.0}
编译没有问题,但会发出警告。但是在void Beziercrove(int,size\t,point\t*)
中,最后一个参数不是只取一个指向一个结构的指针吗?它是一个数组,所以point\t*points包含数组的起始地址。通过索引[1]可以像在数组中一样访问连续点,即