0

考虑一个表示笛卡尔坐标点的结构。

struct point { float x, y; };
typedef struct point point_t;

我有一个函数,它接收一堆点并根据传递的点绘制一条曲线,其定义如下所示,

void beziercurve(int smoothness, size_t n, point_t** points)

我已经编写了函数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...
                                       ^~~~~~

我什至尝试过这样的事情,

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}创建一个指向临时结构的指针,然后在波浪形括号内的一堆结构指针创建一个我可以传递给函数的指针数组。

我错过了什么?为什么代码不起作用?

4

1 回答 1

1

这个复合文字不起作用:

(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}
};

然后您将更改您的函数以获取指向 a 的指针point_t

void beziercurve(int smoothness, size_t n, point_t *points)
于 2018-11-05T17:01:05.770 回答