Dealing with a structure

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Let's say you have a structure:

struct  mystruct {
    unsigned char num;
    unsigned char text[5];
    unsigned int a;
}
struct mystruct ms[] {
   {5,"red",1523},
   {6,"blue",2023},
    ...
   {1,"grn",102},
   {9,"blk",1021}
}

Normally, I would address an element like:

ms[0].a=1523;

However, say that instead of three elements in the struct def I had 50, and the ms struct was 100 deep.
I want some way to programatically parse through the struct and say print each value.
This would mean that I would need to specify all 50 elements:

for (i=0;i<50;i++){
    myprint(ms[i].a);
    myprint(ms[i].text);
   myprint(ms[i].int);
   47 more like statements
)

But to keep code size down I would rather have something like:

n=0;
for (i=0;i<50;i++)(
  myprint(ms[i].n);
  n++;
)

where myprint is a routine that can print any data type.

I'm probably approaching this the wrong way. Any ideas?

Last Edited: Thu. Aug 30, 2012 - 03:57 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Normally, I would address an element like:

But "ms" was not defined as an array? Did you mean:

struct mystruct ms[] {
   {5,"red",1523},
   {6,"blue",2023}, 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry,

That was a mistake. It should read:

struct mystruct ms[] { 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As to your question I don't see an easy solution because even if myprint() is an overloaded C++ function it needs to be invoked separately for each structure element so the compiler knows which overload variant to be invoked at each case.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Assuming the structure is created linearly, can I create a pointer that parses through the struct

ms_rows=50;
myprint( (&ms[0].num + sizeof(ms[])/ms_rows * i + n))

where i is the index to the array row.
Where &ms[0].num is the address of the start of the structure.
n is the current element in a row. For example, 0 corresponds ms[].num, 1 to ms[].text, 6 to ms[].a, etc.
sizeof(ms)/ms_rows * i adds the number of bytes of previous array rows.

The intent is to deliver the address of the byte to print to myprint. the variable n would need to be advanced appropriately depending on the size of the element in the row.

I messed with this for a while, but could not get it to work right.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But your different struct elements are different sizes - you can not just find them using some fixed offset. You could use offsetof() but this still does not help the fact that when the compiler encounters myprint() it has to know the type of the thing that's being printed in order to know which overloaded variant of myprint() to invoke. It can only do this my you making reference to named/typed variables.

Face you are just going to have to write those other 47 lines. But you should be able to take the structure definition and write some editor macro to turn each line into a myprint call.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I will replace the structure with an array and just operate on it as bytes. I'll have a few defines which act as pointers to the element boundaries.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think the desired behaviour you describe is called reflection.

Unfortunately that's no part of C.