Dynamic access to struct field names

Discussion in 'C Programming' started by Andrew Fabbro, Jun 15, 2004.

  1. I think FAQ 2.15 ("How can I access structure fields by name at run
    time?") answers my question, but I don't like the answer ;)

    I have a struct like this:

    struct config_struct {
    char file_dir[MAXPATHLEN];
    char debug_dir[MAXPATHLEN];
    /* and many more */
    } config;

    I have a text file like this:

    file_dir /usr/local/blah/file/
    debug_dir /usr/local/blah/debug

    Parsing this is easy, but populating the config struct is not. Well,
    it's easy but not fun. What I want is something like (pseudocode):

    read in option_name, option_value
    config.(option_name) = option_value (be it by strcpy or whatever)

    It appears there's no way to do this in C. I've looked at some code
    written by far wiser minds than I, and they do a lot of

    if ( strcmp(option_name, "file_dir") == 0 )
    strcpy(config.file_dir, option_value);

    and such. I suppose I could keep an array of field names and their
    sizeofs and then jump around with a pointer...is that really the best
    way?

    -Drew
    Andrew Fabbro, Jun 15, 2004
    #1
    1. Advertising

  2. Andrew Fabbro

    Jack Klein Guest

    On 14 Jun 2004 17:17:29 -0700, (Andrew
    Fabbro) wrote in comp.lang.c:

    > I think FAQ 2.15 ("How can I access structure fields by name at run
    > time?") answers my question, but I don't like the answer ;)
    >
    > I have a struct like this:
    >
    > struct config_struct {
    > char file_dir[MAXPATHLEN];
    > char debug_dir[MAXPATHLEN];
    > /* and many more */
    > } config;
    >
    > I have a text file like this:
    >
    > file_dir /usr/local/blah/file/
    > debug_dir /usr/local/blah/debug
    >
    > Parsing this is easy, but populating the config struct is not. Well,
    > it's easy but not fun. What I want is something like (pseudocode):
    >
    > read in option_name, option_value
    > config.(option_name) = option_value (be it by strcpy or whatever)
    >
    > It appears there's no way to do this in C. I've looked at some code
    > written by far wiser minds than I, and they do a lot of
    >
    > if ( strcmp(option_name, "file_dir") == 0 )
    > strcpy(config.file_dir, option_value);
    >
    > and such. I suppose I could keep an array of field names and their
    > sizeofs and then jump around with a pointer...is that really the best
    > way?
    >
    > -Drew


    If the number of possible names are small, that is probably the best
    way.

    If the number of possible names are large and efficiency is a concern,
    make an array of structures consisting of a pointer to string, an enum
    for different data types, and a ptrdiff_t for the member's offset in
    the structure generated by the standard offsetof macro. Sort the
    array before placing in the source, and bsearch() it to match the
    string.

    It's icky, but perfectly standard and legal C.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jun 15, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob
    Replies:
    1
    Views:
    372
    Lucas Tam
    Jul 30, 2004
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,353
    Chris Fogelklou
    Apr 20, 2004
  3. Zara

    dynamic field names

    Zara, Mar 9, 2006, in forum: C++
    Replies:
    2
    Views:
    437
  4. Sound
    Replies:
    2
    Views:
    428
    Randy Webb
    Sep 28, 2006
  5. jr
    Replies:
    3
    Views:
    405
Loading...

Share This Page