public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Flexible array member initializers
@ 2014-01-08  5:13 Ian Pilcher
  2014-01-08  8:59 ` Jonathan Wakely
  0 siblings, 1 reply; 10+ messages in thread
From: Ian Pilcher @ 2014-01-08  5:13 UTC (permalink / raw)
  To: gcc-help

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
========================================================================

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2014-01-11  2:03 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-01-08  5:13 Flexible array member initializers Ian Pilcher
2014-01-08  8:59 ` Jonathan Wakely
2014-01-08 20:41   ` Ian Pilcher
2014-01-08 21:00     ` Marc Glisse
2014-01-08 21:46       ` Ian Pilcher
2014-01-08 21:43     ` Jonathan Wakely
2014-01-08 21:49       ` Ian Pilcher
2014-01-08 22:21         ` Jonathan Wakely
2014-01-08 23:23     ` Ángel González
2014-01-11  2:03       ` Ian Pilcher

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).