How to initialize an array of structures within a function?

Posted by drtwox on Stack Overflow See other posts from Stack Overflow or by drtwox
Published on 2010-06-13T07:42:14Z Indexed on 2010/06/13 7:52 UTC
Read the original article Hit count: 245

Filed under:

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 {
    uint8_t r,g,b,a;
} rgba_t;

/* Quad polygon - other members removed */
typedef {
    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".

© Stack Overflow or respective owner

Related posts about c