From: Ian Pilcher <arequipeno@gmail.com>
To: gcc-help@gcc.gnu.org
Subject: Flexible array member initializers
Date: Wed, 08 Jan 2014 05:13:00 -0000 [thread overview]
Message-ID: <laimok$i1$1@ger.gmane.org> (raw)
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
========================================================================
next reply other threads:[~2014-01-08 5:13 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-01-08 5:13 Ian Pilcher [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='laimok$i1$1@ger.gmane.org' \
--to=arequipeno@gmail.com \
--cc=gcc-help@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).