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

is it possible to access members of struct in loop?

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
>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

P: n/a
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

P: n/a
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

P: n/a
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.