![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
iso9001
Joined: 02 Dec 2003 Posts: 262
|
Using two different structs for the same data |
Posted: Mon Mar 10, 2008 11:00 pm |
|
|
I'm not sure how to go about this, but I have a struct problem and without assigning pointers to constants I'm not sure what the best way to do this is.
I have two potential data streams coming in. Farm animals in my example
1. Pig, Goat, Monkey, Dog, Cow, Horse
2. Monkey, Pig, Dog, Horse, Goat, Cow
(Aside, I don't know much about farm animals so I ran out and added a monkey in. I tried to sort #1 by size, but hell if I know whats larger between a dog and a monkey, and likewise sorting #2 by intelligence, I'm not sure what goat really ranks, you can teach a horse to do more things so I put that over goat...... geez its late)
At compile time I don't know which data steam I'm going to get and I'd rather not write my functions all twice for either data set. So it would be ideal to use structs for this, but I'm not sure how I can dynamically define a struct like that.
I want to use data.PIG but can't figure out how to make that mean data.[0] and data[1] depending on stream identification,
Ideas ? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Tue Mar 11, 2008 3:29 am |
|
|
Define both structures.
Create a union between the two.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
SimpleAsPossible
Joined: 19 Jun 2004 Posts: 21
|
|
Posted: Mon Mar 17, 2008 10:32 pm |
|
|
Am I understanding this correctly? You are receiving a data stream, and its elements can be in one of two known orders. Then you want to be able to conveniently access the data the way the stream should be interpreted.
So, with a small (ha!) paragraph of disclaimers, here's some code that may help explain.
I assume you have a way to detect which stream type you have received and set the Type variable within the data stream struct. I'm also assuming that the elements are all ints, though they can be other types or structures as needed. I always name all my unions and struct types (usually with prefixes for disambiguation purposes, but not here), probably for the same reason I use parentheses in any expression that gets more complicated than + and -. I also always make Unknown the first element of an enum, as a reminder to initialize it to a reasonable default. I didn't compile this code, it's for illustration only. And yes, it's brute force. It's really more like I'd code on a larger processor, not a PIC. Lots more Get/Set functions are needed.
Code: |
struct s_StreamA
{
int Pig;
int Goat;
int Monkey;
int Dog;
int Cow;
int Horse;
};
struct s_StreamB
{
int Monkey;
int Pig;
int Dog;
int Horse;
int Goat;
int Cow;
};
union u_Stream
{
struct s_StreamA A;
struct s_StreamB B;
};
enum StreamType
{
Unknown,
StreamA,
StreamB
};
struct s_Stream
{
enum StreamType Type;
union u_Stream Data;
}
int GetPig(struct s_Stream s)
{
switch (s.Type)
{
case StreamA:
return s.Data.A.Pig;
break;
case StreamB:
return s.Data.B.Pig;
break;
default:
return 0; // some acceptable default/error
break;
}
}
|
You might also want to look up VARIANT types and see how they're handled. |
|
![](templates/subSilver/images/spacer.gif) |
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Re: Using two different stucts for the same data |
Posted: Tue Mar 18, 2008 11:04 am |
|
|
iso9001 wrote: | I'm not sure how to go about this, but I have a struct problem and without assigning pointers to constants I'm not sure what the best way to do this is.
I have two potential data streams coming in. Farm animals in my example
1. Pig, Goat, Monkey, Dog, Cow, Horse
2. Monkey, Pig, Dog, Horse, Goat, Cow
(Aside, I don't know much about farm animals so I ran out and added a monkey in. I tried to sort #1 by size, but hell if I know whats larger between a dog and a monkey, and likewise sorting #2 by intelligence, I'm not sure what goat really ranks, you can teach a horse to do more things so I put that over goat...... geez its late)
At compile time I don't know which data steam I'm going to get and I'd rather not write my functions all twice for either data set. So it would be ideal to use structs for this, but I'm not sure how I can dynamically define a struct like that.
I want to use data.PIG but can't figure out how to make that mean data.[0] and data[1] depending on stream identification,
Ideas ? | I don't think you could beat a conditional stream converter. If the stream is not the default stream order call a function to rearrange it before using the data. The assembly code generated using this method should be much smaller and run much faster than the structure trick. Your code should be easier to read as well, only having to write your functions once. You could also support several different stream types. This could use a union structure for each stream to keep readability.
I think the structured union access method would be better if you were going to use different functions for each type of stream. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|