473,396 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

referring to struct members by number.

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

struct mystruct {
char member1[3];
char member1[50];
char member1[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.

Dec 13 '06 #1
9 3074

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

struct mystruct {
char member1[3];
char member1[50];
char member1[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.
an array of ptr to char?

char* jagged_array [3];

jagged_array[0] = malloc(3);
if (!jagged_array[0])
abort();

jagged_array[1] = malloc(50);
if (!jagged_array[1])
abort() ;

jagged_array[2] = malloc(12);
if (!jagged_array[2])
abort();

obviously this can be turned into a function

printing:-
for (i = 0; i < 3; i++)
printf ("%s" jagged_array[i]);

code not compiled

--
Nick Keighley

Dec 13 '06 #2
Nick Keighley <ni******************@hotmail.coma écrit*:
>
edson wrote:
>Greetings
For certain operations I would like to have easy access to struct
members. Here is an example.

struct mystruct {
char member1[3];
char member1[50];
char member1[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
}
an array of ptr to char?
3 dynamic allocations, 3 failure tests and 3 malloc overhead :-(
I would really prefer
struct mystruct {
char member1[3];
char member1[50];
char member1[12];
char* memtab[3];
};

void init_mystruct( struct mystruct* s){
s->memtab[0]= &(s->member1);
s->memtab[1]= &(s->member2);
s->memtab[2]= &(s->member3);
}

Marc Boyer
Dec 13 '06 #3
The solution I am using is to (hand) write an array of pointers. This
has the disadvantage that it must be hand edited if the struct is changed.

Is there a risk that another compiler might put the members in different
locations with different offsets from the start of the struct? I am
using GCC and I find that the members are perfectly contiguous with no
gaps between them.
struct mystruct {
char member1[3];
char member2[50];
char member3[12];
};

s_l_fieldpos[] = {0, 3, 50};
/* print the strings in the struct */

for(i=0; i<3; i++){
printf(" %s\r\n", mystruct.member1[s_l_fieldpos[i]]);
}

Dec 13 '06 #4
Le 13-12-2006, edson <ed***@eircom.neta écrit*:
The solution I am using is to (hand) write an array of pointers. This
has the disadvantage that it must be hand edited if the struct is changed.
Yes, like the print_myfunction function. There is no gain.
Is there a risk that another compiler might put the members in different
locations with different offsets from the start of the struct?
Yes. And even gcc could with some optimisation options or
in another version. This is why it exists the 'offsetof' facility.
I am
using GCC and I find that the members are perfectly contiguous with no
gaps between them.

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

s_l_fieldpos[] = {0, 3, 50};
s_l_fieldpos[] = { offsetof(struct mystruct, member1),
offsetof(struct mystruct, member2),
offsetof(struct mystruct, member3) };
/* print the strings in the struct */

for(i=0; i<3; i++){
printf(" %s\r\n", mystruct.member1[s_l_fieldpos[i]]);
printf(" %s\r\n", ((char*)mystruct) + s_l_fieldpos[i]);
}
But, is this really simpler than
void print_myfunction(iconst struct mystruct * s){
printf(" %s\r\n", s->member1);
printf(" %s\r\n", s->member2);
printf(" %s\r\n", s->member3);
}

Marc Boyer
Dec 13 '06 #5
Marc Boyer wrote:
Le 13-12-2006, edson <ed***@eircom.neta écrit :
>>The solution I am using is to (hand) write an array of pointers. This
has the disadvantage that it must be hand edited if the struct is changed.


Yes, like the print_myfunction function. There is no gain.

>>Is there a risk that another compiler might put the members in different
locations with different offsets from the start of the struct?


Yes. And even gcc could with some optimisation options or
in another version. This is why it exists the 'offsetof' facility.

>>I am
using GCC and I find that the members are perfectly contiguous with no
gaps between them.

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

s_l_fieldpos[] = {0, 3, 50};

s_l_fieldpos[] = { offsetof(struct mystruct, member1),
offsetof(struct mystruct, member2),
offsetof(struct mystruct, member3) };
> /* print the strings in the struct */

for(i=0; i<3; i++){
printf(" %s\r\n", mystruct.member1[s_l_fieldpos[i]]);

printf(" %s\r\n", ((char*)mystruct) + s_l_fieldpos[i]);
> }


But, is this really simpler than
void print_myfunction(iconst struct mystruct * s){
printf(" %s\r\n", s->member1);
printf(" %s\r\n", s->member2);
printf(" %s\r\n", s->member3);
}
It is more compact than print_myfunction, especially for a struct with
large number of members - the loop remains the same size with the
s_l_fieldpos[] array becoming just a little longer. It also has the
advantage that const int s_l_fieldpos[] = {0, 3, 50}; is stored in rom
and uses less of the precious ram. (It is a uC program.) s_l_fieldpos[]
= { offsetof(struc... puts the offsets in ram. It is good to discover
the offsetof() function. I didn't know about that.

Dec 13 '06 #6
Le 13-12-2006, edson <ed***@eircom.neta écrit*:
Marc Boyer wrote:
>Le 13-12-2006, edson <ed***@eircom.neta écrit :
But, is this really simpler than
void print_myfunction(iconst struct mystruct * s){
printf(" %s\r\n", s->member1);
printf(" %s\r\n", s->member2);
printf(" %s\r\n", s->member3);
}

It is more compact than print_myfunction, especially for a struct with
large number of members - the loop remains the same size with the
s_l_fieldpos[] array becoming just a little longer. It also has the
advantage that const int s_l_fieldpos[] = {0, 3, 50}; is stored in rom
and uses less of the precious ram.
OK, but you did not explain this constraint in your first post, did
you ?
(It is a uC program.) s_l_fieldpos[]
= { offsetof(struc... puts the offsets in ram. It is good to discover
the offsetof() function. I didn't know about that.
It is not of everyday use, but sometimes, it helps.

Marc Boyer
Dec 13 '06 #7

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

struct mystruct {
char member1[3];
char member1[50];
char member1[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
}

using the boost headers, you can do this in the preprocessor provided
you number all your field names.

#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
#include <stdio.h>

struct mystruct {
char m1[3];
char m2[3];
char m3[3];
};
#define PRINTONE(z, n, stname) printf("%s\n", stname. BOOST_PP_CAT(m,
n) );
#define PRINT(stname) \
do { BOOST_PP_REPEAT_FROM_TO(1, 4, PRINTONE, stname) } while (0)
int main() {
struct mystruct st;
PRINT(st);
}

Dec 13 '06 #8
"tedu" <tu@zeitbombe.orgwrites:
edson wrote:
[snip]
>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
}


using the boost headers, you can do this in the preprocessor provided
you number all your field names.

#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
[snip]

"boost" is not part of standard C. As far as I know, it's not C at
all; I think it's C++.

It *might* be a possible solution for the OP, but if so he'll need to
ask about it in another newsgroup, probably comp.lang.c++ (if boost is
topical there).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 13 '06 #9
Keith Thompson wrote:
"tedu" <tu@zeitbombe.orgwrites:
edson wrote:
[snip]
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
}

using the boost headers, you can do this in the preprocessor provided
you number all your field names.

#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
[snip]

"boost" is not part of standard C. As far as I know, it's not C at
all; I think it's C++.

It *might* be a possible solution for the OP, but if so he'll need to
ask about it in another newsgroup, probably comp.lang.c++ (if boost is
topical there).
the boost preprocessor macros are implemented only in preprocessor. i
know boost itself isn't topical, but it saved me from having to
copy/paste the assorted macros into the posted code (which does compile
with a C compiler) to demonstrate a working example.

Dec 14 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Duncan | last post by:
I need to populate a vector with the following struct details:\ struct group { string groupname; int gid; list<string> members; }; the text file which this is to read from is in the...
15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
5
by: jimjim | last post by:
Hello, Any help will be much appreciatted. My problem is as follows: I declare as global variables: typedef struct _Node{ ..; ..;}Node; Node *Graph; in a function called initiallise(), I...
7
by: Urs Wigger | last post by:
In a C++ project, I have the following struct definition: struct GridModeDataT { double dVal1; double dVal2; double dVal3; long ...
3
by: Erialc Berts | last post by:
Forgive me for being a C# newbie. I have read all of the newsgroup posts, MSDN articles, and other websites on interop and marshalling to send and receive a C struct as a parameter or return...
3
by: GrkEngineer | last post by:
I recently had to use someone's struct from a native app to receive data over Udp. The struct has a array member which looked like this: struct sensorHdr{ char sName; }; When I tried to make...
5
by: jaso | last post by:
Hi, If have a structure of a database record like this: struct record { char id; char title; ... }; Is there some way to find out how many member variables there is in the struct and then...
10
by: Potatoman | last post by:
i have some an array of structs, i need to compare arrays. what can you do me for ?thanx Potatoman
11
by: edson | last post by:
Greetings For certain operations I would like to have easy access to struct members. Here is an example. struct mystruct { char member1; char member2; char member3; };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.