2

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

1
  • Missign semicolon after rgba_t vertex_color[ 4 ] ? Commented Jun 13, 2010 at 7:51

4 Answers 4

4

There were a number of errors in your code, I've corrected them all here. quad_t was already defined in <sys/types.h> so I changed it to myquad_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 memcpy to copy into your dynamically allocated memory.

Here is an explanation of designated initializers.

#include    <stdlib.h>
#include    <string.h>
#include    <stdint.h>

/* RGBA color */
typedef struct rgba {
    uint8_t r,g,b,a;
} rgba_t;

/* Quad polygon - other members removed */
typedef struct quad {
    rgba_t vertex_color[ 4 ];
} myquad_t;

static const myquad_t init_quad = {
     .vertex_color[ 0 ... 3 ] = { 0xFF, 0xFF, 0xFF, 0xFF } 
    };    

myquad_t *make_quad() {
    myquad_t *q = malloc( sizeof( myquad_t ) );
    memcpy(q , &init_quad, sizeof( myquad_t ) );
    return ( q );
}    

int main(void) {

    myquad_t * q = make_quad();

    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Please post the actual code. This, for example:

typedef {
    uint8_t r,g,b,a;
} rgba_t;

won't compile. And it seems you are trying to do assignment, not initialisation. If you had declared the struct properly, you would be able to say:

rgba_t t = { 1,2,3,4 };

but not:

t = { 1,2,3,4 };

2 Comments

I left out the 'struct' keyword when I typed it out. I've fixed it above... and unless I've missed something else, that does compile.
@Artelius not of brace enclose initialisation values.
1

Try

memset(quad, 0xFF, sizeof(quad_t));

Comments

1

In C99 you could do something like

*quad = (quad_t const){
  .vertex_color = {
    { .r = 0xFF, .g = 0xFF, .b = 0xFF, a. = 0xFF },
    { .r = 0xFF, .g = 0xFF, .b = 0xFF, a. = 0xFF },
    { .r = 0xFF, .g = 0xFF, .b = 0xFF, a. = 0xFF },
    { .r = 0xFF, .g = 0xFF, .b = 0xFF, a. = 0xFF },
  }
};

where the right hand side is a so-called compound literal. Any modern compiler should implement this in the most efficient way.

But it would probably be better to be a bit more systematic

#define RGBA_INITIALIZER { .r = 0xFF, .g = 0xFF, .b = 0xFF, a. = 0xFF }
#define QUAD_INITIALIZER {                      \
.vertex_color = {                               \
  RGBA_INITIALIZER, RGBA_INITIALIZER,           \
  RGBA_INITIALIZER, RGBA_INITIALIZER            \
  }                                             \
}

and then just write

*quad = (quad_t const)RGBA_INITIALIZER;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.