473,320 Members | 1,920 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,320 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 3062

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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.