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 1:
printf(" %s\r\n", mystruct.member2 );
break;
case 2:
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.
 
M

Marc Boyer

Le 13-12-2006 said:
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 1:
printf(" %s\r\n", mystruct.member2 );
break;
case 2:
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
}

One problems comes from the type system. I can write a hack based on
offsetof that allows you to write
for(i=0 ; i< 3 ; i++){
puts(member(mystruct,1));
}
but what appends if one is not a char[] ? And you still have to write
the member() function/macro.

If your only need is printing, I think writing and maintaining
member() is as hard as directly writing the print_mystruct() function.
What could be your other needs ?

Marc Boyer
 
C

Chris Dollin

edson wrote:

(Did you know you'd written it /three times/? Please don't do
that.)
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 1:
printf(" %s\r\n", mystruct.member2 );
break;
case 2:
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
}

If you want to access elements by number, use arrays or pointers.

[Trimmed] Real problems are usually more useful than made-up ones.

Why are you putting \r's in your printf output?
 
C

Christopher Benson-Manica

Marc Boyer said:
for(i=0 ; i< 3 ; i++){
puts(member(mystruct,1));
puts(member(mystruct,i)); /* unless member 1 is just that cool */
but what appends if one is not a char[]?

If the members are not all of the same type, the already-tenuous
usefulness of indexed member access really falls apart, unless each
member is itself a struct with some information about how to print it,
which also seems like overkill compared to just writing a
print_mystruct() function.
If your only need is printing, I think writing and maintaining
member() is as hard as directly writing the print_mystruct() function.

Probably harder, since print_mystruct() is more self-documenting.
 
E

edson

Chris said:
edson wrote:

(Did you know you'd written it /three times/? Please don't do
that.)

Sorry about that! I deleted the first two from the newsgroup to make
corrections, but the delete doesn't work in all cases.
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 1:
printf(" %s\r\n", mystruct.member2 );
break;
case 2:
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
}


If you want to access elements by number, use arrays or pointers.

[Trimmed] Real problems are usually more useful than made-up ones.

Why are you putting \r's in your printf output?

I am writing code for a microcontroller which prints to a terminal. I
get stairsteps if I use \n only.
 
E

edson

Chris said:
> edson wrote:
>
> (Did you know you'd written it /three times/? Please don't do
> that.)
>
>
>>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 1:
>> printf(" %s\r\n", mystruct.member2 );
>> break;
>> case 2:
>> 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
>>}
>
> Chris Dollin wrote:
> edson wrote:
>
> (Did you know you'd written it /three times/? Please don't do
> that.)


Sorry about that! I deleted the first two from the newsgroup to make
corrections, but the delete doesn't work in all cases.
>
>> 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 1:
>> printf(" %s\r\n", mystruct.member2 );
>> break;
>> case 2:
>> 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
>> }
>
>
>
> If you want to access elements by number, use arrays or pointers.
>
> [Trimmed] Real problems are usually more useful than made-up ones.
>
> Why are you putting \r's in your printf output?


I am writing code for a microcontroller which prints to a terminal. I
get stairsteps if I use \n only.

> If you want to access elements by number, use arrays or pointers.
>
> [Trimmed] Real problems are usually more useful than made-up ones.
>
> Why are you putting \r's in your printf output?
>
 
R

Random832

2006-12-13 said:
Sorry about that! I deleted the first two from the newsgroup to make
corrections, but the delete doesn't work in all cases.

It hardly works in any cases anymore. A proper supercede message might
be better, but still by no means guaranteed.
I am writing code for a microcontroller which prints to a terminal. I
get stairsteps if I use \n only.

Your stdio library (or terminal driver, or whatever) should be
automatically converting the \n to \n\r for text output to a terminal,
then. A system on which using \n alone causes "stairsteps" is broken and
does not conform to the standard [5.2.2p2]
 
C

Chris Dollin

Random832 said:
2006-12-13 said:
I am writing code for a microcontroller which prints to a terminal. I
get stairsteps if I use \n only.

Your stdio library (or terminal driver, or whatever) should be
automatically converting the \n to \n\r for text output to a terminal,
then. A system on which using \n alone causes "stairsteps" is broken and
does not conform to the standard [5.2.2p2]

If he's writing code for a microcontroller, he's probably using freestanding
C. Does the Standard constrain freestanding implementations to have the
same meaning for functions that have the same name as those in hosted
implementations?

I would call that implementation (the one edson is using) "unhelpful",
though. It's misleading at best to provide a printf that doesn't work
the way Standard::printf works, even if it's legal.
 
E

edson

Just a thought:
Are the addresses of the struct members stored somewhere so that I could
access them?
Fe if I have a statement i = (int) mystruct.member2, I will get the
location of member2. Does the program calculate it on demand, or
retrieve it? If the latter, could I retrieve it if I know where it is
stored?
 
E

edson

Default said:
edson wrote:





Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:

Point taken.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top