简体   繁体   中英

How to initialize an array of structures within a function?

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

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

尝试

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM