Flexible array member initializers

Ian Pilcher arequipeno@gmail.com
Wed Jan 8 05:13:00 GMT 2014


I am working on what I hope will be a very simple C configuration file
parsing library/module.  (Because the world obviously needs another
one!)  I define the acceptable options in terms of flexible types:

struct foo_type {
    const char *name;
    int (*parse_fn)(void *dest, const char *text);
    int (*format_fn)(char *buf, size_t buf_size, const void *value);
    void (*free_fn)(void *default_value);
    size_t size;
};

struct foo_opt {
    const char *name;
    const struct foo_type *type;
    unsigned char flags;
    unsigned char def_val[];
};

Note the use of the flexible array member to hold the default value (or
possible a pointer to the default value, depending on the type.)

I'm trying to figure out if there is any way to statically initialize a
struct foo_opt with a default value that is not an array of unsigned
characters.  For example:

struct foo_type foo_int = {
    .name       = "integer",
    .parse_fn   = foo_parse_int,
    .format_fn  = foo_format_int,
    .free_fn    = 0,
    .size       = sizeof(int),
};

struct foo_opt foo_option = {
    .name       = "max_workers",
    .type       = &foo_int,
    .flags      = FOO_OPT_HAS_DEFAULT,
    .def_val    = (unsigned char[])5,
};

My goal is to provide some sort of macro which will make it (reasonably)
simple to provide static initializers for the foo_opt structs -- without
replacing the flexible array member with a void *.

Is there any way to do this?

Thanks!

-- 
========================================================================
Ian Pilcher                                         arequipeno@gmail.com
           Sent from the cloud -- where it's already tomorrow
========================================================================



More information about the Gcc-help mailing list