Consider a structure that represents a point in cartesian co-ordinate.
struct point { float x, y; };
typedef struct point point_t;
I have a function that takes in a bunch of points and draws a curve based on the points passed, whose definition looks like this,
void beziercurve(int smoothness, size_t n, point_t** points)
I have written the function bezier and I want to test if my function is working correctly. So, inside the main function, I passed the following dummy values to the function via compound literals,
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 gives me the following error,
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...
^~~~~~
I even tried something like this,
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);
But that also doesn't work. My logic is like this: (point_t*){.x=1.0, .y=1.0} creates a pointer to a temporary structure and a bunch of these structure pointers inside squiggly brackets creates an array of those pointers that I can pass to the function.
What am I missing? Why is the code not working?
(point_t*){.x=1.0, .y=1.0}creates a pointer to a temporary structure - Pointer does not have fields, so your syntax is simply not valid. You might create a compound literal of the structure itself and pass it's address such as&(point_t){.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}};compiles okay with warning but throws me a SIGSEGV.