In the make_quad() function below, how do I set the default values for the vertex_color array in the quad_t structure?
/* RGBA color */
typedef struct {
uint8_t r,g,b,a;
} rgba_t;
/* Quad polygon - other members removed */
typedef struct {
rgba_t vertex_color[ 4 ];
} quad_t;
Elsewhere, a function to make and init a quad:
quad_t *make_quad() {
quad_t *quad = malloc( sizeof( quad_t ) );
quad->vertex_color = ??? /* What goes here? */
return ( quad );
}
Obviously I can do it like this:
quad->vertex_color[ 0 ] = { 0xFF, 0xFF, 0xFF, 0xFF };
...
quad->vertex_color[ 3 ] = { 0xFF, 0xFF, 0xFF, 0xFF };
but this:
quad->vertex_color = {
{ 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF }
};
…results in “error: expected expression before ‘{‘ token“.
EDIT: Fixed a couple typos
There were a number of errors in your code, I’ve corrected them all here.
quad_twas already defined in<sys/types.h>so I changed it tomyquad_t.Basically, you can’t statically init memory that’s dynamically allocated at run-time, so what you can do is create a global which is initialized at compile time which you then use
memcpyto copy into your dynamically allocated memory.Here is an explanation of designated initializers.