access struct member fields dynamically

S

sean.scanlon

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'

is there a way to treat fields 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, foo->fields);

}

return 0;
}


Thanks for any help,
-sean
 
M

Morris Dovey

(e-mail address removed) (in
(e-mail address removed)) said:

| 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'
|
| is there a way to treat fields 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, foo->fields);
|
| }
|
| 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, foo->fields);
}
return 0;
}
 
G

Giorgio Silvestri

Morris Dovey said:
(e-mail address removed) (in
(e-mail address removed)) said:

| 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'
|
| is there a way to treat fields 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, foo->fields);
|
| }
|
| 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, foo->fields);
}
return 0;
}


Segfault!
foo->fields is NULL.
 
M

Morris Dovey

Giorgio Silvestri (in [email protected]) said:

| "Morris Dovey" <[email protected]> ha scritto nel messaggio
| || (e-mail address removed) (in
|| (e-mail address removed)) 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, foo->fields);
|| }
|| return 0;
|| }
||
|
| Segfault!
| foo->fields 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, foo->fields ?
foo->fields : "(null)");

would certainly be in order.
 
F

Fred Kleinschmidt

Giorgio Silvestri said:
Morris Dovey said:
(e-mail address removed) (in
(e-mail address removed)) said:

| 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'
|
| is there a way to treat fields 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, foo->fields);
|
| }
|
| 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, foo->fields);
}
return 0;
}


Segfault!
foo->fields is NULL.


No. foo itself has not been pointed to anything.
Therefore dereferencing foo leads to bad things.
 
K

Keith Thompson

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'

is there a way to treat fields 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, foo->fields);

}

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).
 
T

Thomas Matthews

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'

is there a way to treat fields 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, foo->fields);

}

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
 
S

swets

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.

Any comments?

Sweta
 
G

Giorgio Silvestri

Keith Thompson said:
(e-mail address removed) 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!
 
G

Giorgio Silvestri

Fred Kleinschmidt said:
Giorgio Silvestri said:
Morris Dovey said:
(e-mail address removed) (in
(e-mail address removed)) said:

| 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'
|
| is there a way to treat fields 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, foo->fields);
|
| }
|
| 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, foo->fields);
}
return 0;
}


Segfault!
foo->fields 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;
/* ... */
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top