By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,543 Members | 2,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,543 IT Pros & Developers. It's quick & easy.

access struct member fields dynamically

P: n/a
can someone help understand how i can could access a struct field
dymanically like: foo->fields[i] ?

when i try to compile this i get the following error: 'struct pwd' has
no member named 'fields'

is there a way to treat fields[i] as the member name of the struct?

example code:

struct pwd {
char *first;
char *last;
char *bar;
};

struct pwd *foo;

char* fields[] = {"first", "last", "bar"};

int main (int argc, char* argv[ ]) {

int i;

const int f_size = ( sizeof fields ) / ( sizeof fields[0] );

for (i = 0; i<f_size;i++) {

printf("%d == %s (%s)\n", i, fields[i], foo->fields[i]);

}

return 0;
}
Thanks for any help,
-sean

Aug 10 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
se**********@gmail.com (in
11*********************@b28g2000cwb.googlegroups.c om) said:

| can someone help understand how i can could access a struct field
| dymanically like: foo->fields[i] ?
|
| when i try to compile this i get the following error: 'struct pwd'
| has no member named 'fields'
|
| is there a way to treat fields[i] as the member name of the struct?
|
|
|
| example code:
|
| struct pwd {
| char *first;
| char *last;
| char *bar;
| };
|
| struct pwd *foo;
|
| char* fields[] = {"first", "last", "bar"};
|
| int main (int argc, char* argv[ ]) {
|
| int i;
|
| const int f_size = ( sizeof fields ) / ( sizeof fields[0] );
|
| for (i = 0; i<f_size;i++) {
|
| printf("%d == %s (%s)\n", i, fields[i], foo->fields[i]);
|
| }
|
| return 0;
| }

How about something like:

#define first fields[0]
#define last fields[1]
#define bar fields[2]

struct pwd
{ char *fields[3];
} = { NULL,NULL,NULL };

struct pwd *foo;
char *names[] = {"first", "last", "bar"};

int main (void)
{ int i;
const int f_size = ( sizeof names ) / ( sizeof names[0] );

for (i = 0; i<f_size; i++)
{ printf("%d == %s (%s)\n", i, names[i], foo->fields[i]);
}
return 0;
}

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Aug 10 '06 #2

P: n/a

"Morris Dovey" <mr*****@iedu.comha scritto nel messaggio
news:44***********************@news.qwest.net...
se**********@gmail.com (in
11*********************@b28g2000cwb.googlegroups.c om) said:

| can someone help understand how i can could access a struct field
| dymanically like: foo->fields[i] ?
|
| when i try to compile this i get the following error: 'struct pwd'
| has no member named 'fields'
|
| is there a way to treat fields[i] as the member name of the struct?
|
|
|
| example code:
|
| struct pwd {
| char *first;
| char *last;
| char *bar;
| };
|
| struct pwd *foo;
|
| char* fields[] = {"first", "last", "bar"};
|
| int main (int argc, char* argv[ ]) {
|
| int i;
|
| const int f_size = ( sizeof fields ) / ( sizeof fields[0] );
|
| for (i = 0; i<f_size;i++) {
|
| printf("%d == %s (%s)\n", i, fields[i], foo->fields[i]);
|
| }
|
| return 0;
| }

How about something like:

#define first fields[0]
#define last fields[1]
#define bar fields[2]
Unused.
>
struct pwd
{ char *fields[3];
} = { NULL,NULL,NULL };
Missing variable:

struct pwd {
char *fields[3];
} var = { {NULL,NULL,NULL} };

>
struct pwd *foo;
char *names[] = {"first", "last", "bar"};

int main (void)
{ int i;
const int f_size = ( sizeof names ) / ( sizeof names[0] );

for (i = 0; i<f_size; i++)
{ printf("%d == %s (%s)\n", i, names[i], foo->fields[i]);
}
return 0;
}
Segfault!
foo->fields[i] is NULL.
--
Giorgio Silvestri
DSP/Embedded/Real Time OS Software Engineer

Aug 10 '06 #3

P: n/a
Giorgio Silvestri (in F5********************@twister2.libero.it) said:

| "Morris Dovey" <mr*****@iedu.comha scritto nel messaggio
| news:44***********************@news.qwest.net...
|| se**********@gmail.com (in
|| 11*********************@b28g2000cwb.googlegroups.c om) said:
||
|| How about something like:
||
|| #define first fields[0]
|| #define last fields[1]
|| #define bar fields[2]
|
| Unused.

But useful for getting the idea across.

|| struct pwd
|| { char *fields[3];
|| } = { NULL,NULL,NULL };
|
| Missing variable:

Good catch!

| struct pwd {
| char *fields[3];
| } var = { {NULL,NULL,NULL} };
|
|| struct pwd *foo;
|| char *names[] = {"first", "last", "bar"};
||
|| int main (void)
|| { int i;
|| const int f_size = ( sizeof names ) / ( sizeof names[0] );
||
|| for (i = 0; i<f_size; i++)
|| { printf("%d == %s (%s)\n", i, names[i], foo->fields[i]);
|| }
|| return 0;
|| }
||
|
| Segfault!
| foo->fields[i] is NULL.

Exactly so; but I would guess that the OP intends to actually use
those pointers for something. If not, then a test

printf("%d == %s (%s)\n", i, names[i], foo->fields[i] ?
foo->fields[i] : "(null)");

would certainly be in order.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Aug 10 '06 #4

P: n/a

"Giorgio Silvestri" <gi**************@libero.itwrote in message
news:F5********************@twister2.libero.it...
>
"Morris Dovey" <mr*****@iedu.comha scritto nel messaggio
news:44***********************@news.qwest.net...
>se**********@gmail.com (in
11*********************@b28g2000cwb.googlegroups.c om) said:

| can someone help understand how i can could access a struct field
| dymanically like: foo->fields[i] ?
|
| when i try to compile this i get the following error: 'struct pwd'
| has no member named 'fields'
|
| is there a way to treat fields[i] as the member name of the struct?
|
|
|
| example code:
|
| struct pwd {
| char *first;
| char *last;
| char *bar;
| };
|
| struct pwd *foo;
|
| char* fields[] = {"first", "last", "bar"};
|
| int main (int argc, char* argv[ ]) {
|
| int i;
|
| const int f_size = ( sizeof fields ) / ( sizeof fields[0] );
|
| for (i = 0; i<f_size;i++) {
|
| printf("%d == %s (%s)\n", i, fields[i], foo->fields[i]);
|
| }
|
| return 0;
| }

How about something like:

#define first fields[0]
#define last fields[1]
#define bar fields[2]

Unused.
>>
struct pwd
{ char *fields[3];
} = { NULL,NULL,NULL };

Missing variable:

struct pwd {
char *fields[3];
} var = { {NULL,NULL,NULL} };

>>
struct pwd *foo;
char *names[] = {"first", "last", "bar"};

int main (void)
{ int i;
const int f_size = ( sizeof names ) / ( sizeof names[0] );

for (i = 0; i<f_size; i++)
{ printf("%d == %s (%s)\n", i, names[i], foo->fields[i]);
}
return 0;
}

Segfault!
foo->fields[i] is NULL.
No. foo itself has not been pointed to anything.
Therefore dereferencing foo leads to bad things.
>

--
Giorgio Silvestri
DSP/Embedded/Real Time OS Software Engineer
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Aug 10 '06 #5

P: n/a
se**********@gmail.com writes:
can someone help understand how i can could access a struct field
dymanically like: foo->fields[i] ?

when i try to compile this i get the following error: 'struct pwd' has
no member named 'fields'

is there a way to treat fields[i] as the member name of the struct?

example code:

struct pwd {
char *first;
char *last;
char *bar;
};

struct pwd *foo;

char* fields[] = {"first", "last", "bar"};
Identifiers in your source code do not exist during execution.
There's no direct way to map from the string "first" to the identifier
first. (It's not inconceivable that your implementation might provide
some way of doing to, most likely for use by source-level debuggers,
but if so I strongly advise against trying to use it.)
int main (int argc, char* argv[ ]) {

int i;

const int f_size = ( sizeof fields ) / ( sizeof fields[0] );

for (i = 0; i<f_size;i++) {

printf("%d == %s (%s)\n", i, fields[i], foo->fields[i]);

}

return 0;
}
There's a very simple and straightforward way to refer to the members
as array elements: Declare them that way.

struct pwd {
char *fields[3];
};

If you want to be able to refer to them either as array elements or as
named fields, the first thing you should do is stop and consider
whether you really want to do that. In most cases, it only makes
sense to do one or the other. (And, of course, the members of a
structure needn't all be of the same type.)

You might be tempted to use a union to overlay your array with a
sub-struct containing the individual members. Resist this temptation.
There's no guarantee that the layout will match. Worse, it *probably*
will match, meaning that your code will have a potential bug that you
probably won't be able to detect in testing.

As someone else suggested, you can use macros to make the array
elements look like individual struct members -- but since macros just
do textual substitution, you may eventually run into a situation where
things mysteriously don't work.

Or you can declare an enumerated type and use the enumerators as
indices:

enum index_type { FIRST, LAST, BAR, ARRAY_LENGTH=BAR+1 };

struct pwd {
char *fields[ARRAY_LENGTH];
};

struct pwd obj;

... obj.fields[BAR] ...

(I haven't tested this.)

Whenever possible, work with the language rather than trying to warp
it to your purposes (though sometimes that's the only solution).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 10 '06 #6

P: n/a
se**********@gmail.com wrote:
can someone help understand how i can could access a struct field
dymanically like: foo->fields[i] ?

when i try to compile this i get the following error: 'struct pwd' has
no member named 'fields'

is there a way to treat fields[i] as the member name of the struct?

example code:

struct pwd {
char *first;
char *last;
char *bar;
};

struct pwd *foo;

char* fields[] = {"first", "last", "bar"};

int main (int argc, char* argv[ ]) {

int i;

const int f_size = ( sizeof fields ) / ( sizeof fields[0] );

for (i = 0; i<f_size;i++) {

printf("%d == %s (%s)\n", i, fields[i], foo->fields[i]);

}

return 0;
}
Thanks for any help,
-sean
Here is some untested code demonstrating the concept of storing
the name of a field along with its offset:

struct pwd {
char *first;
char *last;
char *bar;
};

struct Name_Offset
{
const char * field_name;
size_t field_offset;
};

const struct Name_Offset offset_table[] =
{
{"first", offsetof(struct pwd.first)},
{"last", offsetof(struct pwd.last)},
{"bar", offsetof(struct pwd.bar)}
};
const size_t Field_Quantity =
sizeof(offset_table) / sizeof(offset_table[0]);

The big issue with accessing fields by name is that you have
to convert the name into an offset into the structure, then
access the field.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Aug 11 '06 #7

P: n/a
You can try this way too -
struct pwd {
union {
struct {
char *first;
char *last;
char *bar;
}s;
char *fields[3];
}u;
};
};
and then you can access fields like foo->u.fields[i].

Any comments?

Sweta

se**********@gmail.com wrote:
can someone help understand how i can could access a struct field
dymanically like: foo->fields[i] ?

when i try to compile this i get the following error: 'struct pwd' has
no member named 'fields'

is there a way to treat fields[i] as the member name of the struct?

example code:

struct pwd {
char *first;
char *last;
char *bar;
};

struct pwd *foo;

char* fields[] = {"first", "last", "bar"};

int main (int argc, char* argv[ ]) {

int i;

const int f_size = ( sizeof fields ) / ( sizeof fields[0] );

for (i = 0; i<f_size;i++) {

printf("%d == %s (%s)\n", i, fields[i], foo->fields[i]);

}

return 0;
}
Thanks for any help,
-sean
Aug 11 '06 #8

P: n/a

"Keith Thompson" <ks***@mib.orgha scritto nel messaggio
news:ln************@nuthaus.mib.org...
se**********@gmail.com writes:
[...]
There's a very simple and straightforward way to refer to the members
as array elements: Declare them that way.

struct pwd {
char *fields[3];
};

If you want to be able to refer to them either as array elements or as
named fields, the first thing you should do is stop and consider
whether you really want to do that. In most cases, it only makes
sense to do one or the other. (And, of course, the members of a
structure needn't all be of the same type.)

You might be tempted to use a union to overlay your array with a
sub-struct containing the individual members. Resist this temptation.
There's no guarantee that the layout will match. Worse, it *probably*
will match, meaning that your code will have a potential bug that you
probably won't be able to detect in testing.

As someone else suggested, you can use macros to make the array
elements look like individual struct members -- but since macros just
do textual substitution, you may eventually run into a situation where
things mysteriously don't work.

Or you can declare an enumerated type and use the enumerators as
indices:

enum index_type { FIRST, LAST, BAR, ARRAY_LENGTH=BAR+1 };

struct pwd {
char *fields[ARRAY_LENGTH];
};

struct pwd obj;

... obj.fields[BAR] ...
this version is simpler and probably more parametric:

enum index_type { FIRST, LAST, BAR, ARRAY_LENGTH };

I can also change, the order:

enum index_type { FIRST, BAR, LAST , ARRAY_LENGTH };

ARRAY_LENGTH is always the last of course!


--
Giorgio Silvestri
DSP/Embedded/Real Time OS Software Engineer

Aug 11 '06 #9

P: n/a

"Fred Kleinschmidt" <fr******************@boeing.comha scritto nel
messaggio news:J3********@news.boeing.com...
>
"Giorgio Silvestri" <gi**************@libero.itwrote in message
news:F5********************@twister2.libero.it...

"Morris Dovey" <mr*****@iedu.comha scritto nel messaggio
news:44***********************@news.qwest.net...
se**********@gmail.com (in
11*********************@b28g2000cwb.googlegroups.c om) said:

| can someone help understand how i can could access a struct field
| dymanically like: foo->fields[i] ?
|
| when i try to compile this i get the following error: 'struct pwd'
| has no member named 'fields'
|
| is there a way to treat fields[i] as the member name of the struct?
|
|
|
| example code:
|
| struct pwd {
| char *first;
| char *last;
| char *bar;
| };
|
| struct pwd *foo;
|
| char* fields[] = {"first", "last", "bar"};
|
| int main (int argc, char* argv[ ]) {
|
| int i;
|
| const int f_size = ( sizeof fields ) / ( sizeof fields[0] );
|
| for (i = 0; i<f_size;i++) {
|
| printf("%d == %s (%s)\n", i, fields[i], foo->fields[i]);
|
| }
|
| return 0;
| }

How about something like:

#define first fields[0]
#define last fields[1]
#define bar fields[2]
Unused.
>
struct pwd
{ char *fields[3];
} = { NULL,NULL,NULL };
Missing variable:

struct pwd {
char *fields[3];
} var = { {NULL,NULL,NULL} };

>
struct pwd *foo;
char *names[] = {"first", "last", "bar"};

int main (void)
{ int i;
const int f_size = ( sizeof names ) / ( sizeof names[0] );

for (i = 0; i<f_size; i++)
{ printf("%d == %s (%s)\n", i, names[i], foo->fields[i]);
}
return 0;
}
Segfault!
foo->fields[i] is NULL.

No. foo itself has not been pointed to anything.
Therefore dereferencing foo leads to bad things.
Thank you. Of course I suppose:

/* ... */
foo = &var;
/* ... */


--
Giorgio Silvestri
DSP/Embedded/Real Time OS Software Engineer


Aug 11 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.