linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jan-Benedict Glaw <jbglaw@lug-owl.de>
To: linux-c-programming@vger.kernel.org
Subject: Re: named structure members
Date: Thu, 11 Sep 2003 18:52:13 +0200	[thread overview]
Message-ID: <20030911165213.GJ14376@lug-owl.de> (raw)
In-Reply-To: <16224.39341.204427.234601@cerise.nosuchdomain.co.uk>

[-- Attachment #1: Type: text/plain, Size: 1618 bytes --]

On Thu, 2003-09-11 16:50:05 +0100, Glynn Clements <glynn.clements@virgin.net>
wrote in message <16224.39341.204427.234601@cerise.nosuchdomain.co.uk>:
> Matthew Harrison wrote:
> > here is the struct that i want my config to go into:
> There are a number of ways which you could improve upon this, but
> there isn't anything along the lines of:
> 
> 	config.<something involving "keyword"> = value;
> 
> Structure fields have to be specified explicitly; you can't "index" a
> structure in the manner of an associative array.
> 
> One possible solution is:
> 
> enum cfg_option {
> 	cfg_db_host,
> 	cfg_db_name,
> 	cfg_db_pass,
> 	cfg_db_user,
> 	CFG_COUNT
> };
[...]
Another approach is to use a union (containing any possible things, in
your case only char pointers) inside a struct (which also contains a
char name[] and an int type). Build up an array with these structs, one
for each config option.

Then have a function which gets on option name as well as an option
value which searches throuch all ->name's of your array and assigns the
(properly converted) value to the proper union type.

To access these options, use multiple access functions (ie.

int get_int(char *option)

) which also searches through the array and returns the int part of the
union.

MfG, JBG

-- 
   Jan-Benedict Glaw       jbglaw@lug-owl.de    . +49-172-7608481
   "Eine Freie Meinung in  einem Freien Kopf    | Gegen Zensur | Gegen Krieg
    fuer einen Freien Staat voll Freier Bürger" | im Internet! |   im Irak!
      ret = do_actions((curr | FREE_SPEECH) & ~(IRAQ_WAR_2 | DRM | TCPA));

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

  reply	other threads:[~2003-09-11 16:52 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-09-11 11:07 named structure members Matthew Harrison
2003-09-11 11:50 ` Jeff Woods
2003-09-11 14:03 ` Mariano Moreyra
2003-09-11 14:26   ` Matthew Harrison
2003-09-11 14:41     ` Mariano Moreyra
2003-09-11 15:28       ` Matthew Harrison
2003-09-11 15:35         ` Mariano Moreyra
2003-09-11 15:42           ` Matthew Harrison
2003-09-11 15:50     ` Glynn Clements
2003-09-11 16:52       ` Jan-Benedict Glaw [this message]
2003-09-11 17:13         ` Matthew Harrison
2003-09-12 13:22           ` Mariano Moreyra
2003-09-12 13:47             ` Matthew Harrison
2003-09-12 13:52               ` Mariano Moreyra
2003-09-12 14:33                 ` Matthew Harrison

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=20030911165213.GJ14376@lug-owl.de \
    --to=jbglaw@lug-owl.de \
    --cc=linux-c-programming@vger.kernel.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 NNTP newsgroup(s).