473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Dynamic access to struct field names

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
Nov 14 '05 #1
1 5568
On 14 Jun 2004 17:17:29 -0700, an******************@fabbro.org (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
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
5
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
11
by: Marco Loskamp | last post by:
Dear list, I'm trying to dynamically generate functions; it seems that what I really want is beyond C itself, but I'd like to be confirmed here. In the minimal example below, I'd like to...
9
by: Jimbo | last post by:
Hello, I have a user request to build a form in an Access database where the user can check off specific fields to pull in a query. For example, let's say I have 10 fields in a table. The user...
4
by: alexandre.brisebois | last post by:
Hi, I am using access 2003, I would like to know if there is an option to reorganize the tables in a maner that is readable, as we can do in sql sever 2000 or 2005. I have been given a database...
9
by: sean.scanlon | last post by:
can someone help understand how i can could access a struct field dymanically like: foo->fields ? when i try to compile this i get the following error: 'struct pwd' has no member named 'fields'...
13
by: salad | last post by:
Operating in A97. I didn't receive much of a response conserning Pivot tables in Access. Pivot tables are nice, but a CrossTab will work for me too. Using a Pivot table, one is actually...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
6
by: castironpi | last post by:
struct.Struct lets you encode Python objects into structured memory. It accepts a format string, and optionally a buffer and offset to/from which to read/write the structure. What do you think of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.