Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Dynamic access to struct field names

Reply
Thread Tools

Dynamic access to struct field names

 
 
Andrew Fabbro
Guest
Posts: n/a
 
      06-15-2004
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
 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      06-15-2004
On 14 Jun 2004 17:17:29 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
1.Enter space bar for field names and save the field.The field shoud not get saved and an alert should be there as"Space bars are not allowed" Sound Javascript 2 09-28-2006 02:43 PM
dynamic field names Zara C++ 2 03-09-2006 02:33 PM
table field names vs. display names Bob ASP .Net 1 07-30-2004 05:06 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments