Re: is it possible to access members of struct in loop?



On 23 Jun 2006 00:12:02 -0700, "Erich Pul" <erich.pul@xxxxxxxxxxxx>
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
.



Relevant Pages

  • Re: Comparing pointers to NULL
    ... Suppose our data is an array of records with each record having many ... In C it is natural to use a struct to represent a record and an ... in the body of a loop over record number the current record number ...
    (comp.lang.c)
  • Re: extract items from list in single field
    ... Try using the Splitfunction to change the delimited string into an array. ... You would then loop through the members of the array and write one record to ...
    (microsoft.public.access.modulesdaovba)
  • Re: #defining an array within another in C language
    ... you do not know the number of elements of the array. ...     int var; ... C99 flexible array members or the plain old "struct hack". ...
    (comp.lang.c)
  • Re: C, strcat
    ... Actually a 1-length array of char. ... >members (all char strings) contained in a structure. ... struct member_data ...
    (alt.comp.lang.learn.c-cpp)
  • Re: structure toupperlower?
    ... >> I need to properly format the case of a struct. ... Are you trying to iterate over the members of a struct? ... you probably can if you first build an array each element ... The toupperand tolowerfunctions apply to a single character: ...
    (comp.lang.c)

Loading