referring to struct members by number.

E

edson

Greetings
For certain operations I would like to have easy access to struct
members. Here is an example.

struct mystruct {
char member1[3];
char member2[50];
char member3[12];
};

/* print the strings in the struct */

for(i=0; i<3; i++){
switch(i):
case 0:
printf(" %s\r\n", mystruct.member1 );
break;
case 0:
printf(" %s\r\n", mystruct.member2 );
break;
case 0:
printf(" %s\r\n", mystruct.member3 );
break;
}

This is very cumbersom, sepecially for large structs. Is there some way
I could avoid having to name each member?
something like the following pseudocode;

for(i=0; i<3; i++){
print member i
}

I would appreciate advice on this.
Regards.
 
D

Duncan Muirhead

Greetings
For certain operations I would like to have easy access to struct
members. Here is an example.
If I was going to have to do this a lot, I'd be tempted to write
a wee program that would itself write the structure definition and
required operations for inclusion in the real code. For example
the wee program might take as input "mystruct char member1 3; char
member 2 50;" and produce as output
struct mystruct { char member1[3]; char member2[50]};
void print_mystruct( struct mystruct* S) { /* body */};

Duncan
 

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,780
Messages
2,569,608
Members
45,242
Latest member
KendrickKo

Latest Threads

Top