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.member 1 );
break;
case 0:
printf(" %s\r\n", mystruct.member 2 );
break;
case 0:
printf(" %s\r\n", mystruct.member 3 );
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. 9 3102
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.member 1 );
break;
case 0:
printf(" %s\r\n", mystruct.member 2 );
break;
case 0:
printf(" %s\r\n", mystruct.member 3 );
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
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.member 1 ); break; case 0: printf(" %s\r\n", mystruct.member 2 ); break; case 0: printf(" %s\r\n", mystruct.member 3 ); 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
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.member 1[s_l_fieldpos[i]]);
}
Le 13-12-2006, edson <ed***@eircom.n eta é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_myfunctio n 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.member 1[s_l_fieldpos[i]]);
printf(" %s\r\n", ((char*)mystruc t) + s_l_fieldpos[i]);
}
But, is this really simpler than
void print_myfunctio n(iconst struct mystruct * s){
printf(" %s\r\n", s->member1);
printf(" %s\r\n", s->member2);
printf(" %s\r\n", s->member3);
}
Marc Boyer
Marc Boyer wrote:
Le 13-12-2006, edson <ed***@eircom.n eta é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_myfunctio n 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_fieldpo s[] = {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.member 1[s_l_fieldpos[i]]);
printf(" %s\r\n", ((char*)mystruc t) + s_l_fieldpos[i]);
> }
But, is this really simpler than
void print_myfunctio n(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_myfunctio n, 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.
Le 13-12-2006, edson <ed***@eircom.n eta écrit*:
Marc Boyer wrote:
>Le 13-12-2006, edson <ed***@eircom.n eta écrit : But, is this really simpler than void print_myfunctio n(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_myfunctio n, 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
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.member 1 );
break;
case 0:
printf(" %s\r\n", mystruct.member 2 );
break;
case 0:
printf(" %s\r\n", mystruct.member 3 );
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);
}
"tedu" <tu@zeitbombe.o rgwrites:
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_Keit h) 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.
Keith Thompson wrote:
"tedu" <tu@zeitbombe.o rgwrites:
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 following form:
|
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 the purpose of learning
to work with C++. It therefore makes some sense for me to give the
situation the amount of consideration presented below. To be quite honest,
I'm amazed at the amount there is to say about such a seemingly simple...
|
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 allocate memory and copy information:
for(i )
|
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 lNumberOfPoints;
int bUseRegion;
enum ES_RegionType regionType;
|
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 value in a C function call from C#.
My situation is slightly different, because the C struct I have is global.
I have tried different approaches/techniques (string, String, std::string,
IntPtr, Marshal, class, etc.), but none work. I was given a...
| |
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 this a managed struct by adding either the value struct
or ref struct I received a compile error stating that mixed types are not
supported. I understand this, but I don't know how to create a char array in
|
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 iterate through them?
|
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
|
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;
};
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |