468,727 Members | 1,389 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,727 developers. It's quick & easy.

is it possible to access members of struct in loop?

hi!

is it possible to access the members of a struct (e.g. 10 members) in a
loop, comparable to an array?

tia,

E

Jun 23 '06 #1
4 9998
>is it possible to access the members of a struct (e.g. 10 members) in a
loop, comparable to an array?


If you're expecting to iterate over the members of unknown name and
type, NO. You can make a table of the member names, types, and
offset (using offsetof()), and use that, but you have to keep it
up to date with the real struct, and it's messy.

If you need this, you probably want an array as a struct member.

Gordon L. Burditt
Jun 23 '06 #2
Erich Pul wrote:
hi!

is it possible to access the members of a struct (e.g. 10 members) in a
loop, comparable to an array?

No (unless those 10 members happen to be an array!), C has nothing like
the foraech construct found in some other languages.

--
Ian Collins.
Jun 23 '06 #3
No (unless those 10 members happen to be an array!), C has nothing like
the foraech construct found in some other languages.


thank you - i will pack the values in an array and have my loop go
through it

E

Jun 23 '06 #4
On 23 Jun 2006 00:12:02 -0700, "Erich Pul" <er*******@blackbox.net>
wrote:
hi!

is it possible to access the members of a struct (e.g. 10 members) in a
loop, comparable to an array?
As others have said, no. There are two things you can do that may, or
may not, approach this closely enough for your purpose:

enum { height /* = 0 */, width /* = 1 */, depth /* etc. */, NUMDIM };
struct dims { double dim [NUMDIM /* or 3 */]; } foo;

.... volume = foo.dim[height] * foo.dim[width] * foo.dim[depth]; ...

/* best not to use simple names like height because a macro
takes the identifier 'out of play' for ALL other use */
#define d_height dim[0]
#define d_width dim[1]
#define d_depth dim[2]
#define d_NUMDIM 3
struct dims { double dim [NUMDIM]; } foo;
.... volume = foo.d_height * foo.d_width * foo.d_depth; ...
- David.Thompson1 at worldnet.att.net
Jul 10 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Random Person | last post: by
5 posts views Thread by jimjim | last post: by
1 post views Thread by Michael B Allen | last post: by
9 posts views Thread by edson | last post: by
2 posts views Thread by A n g l e r | last post: by
6 posts views Thread by noone | last post: by
5 posts views Thread by Mahendra Kumar Kutare | last post: by
1 post views Thread by CARIGAR | last post: by
9 posts views Thread by bryonone | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.