CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Using two different structs for the same data

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
iso9001



Joined: 02 Dec 2003
Posts: 262

View user's profile Send private message

Using two different structs for the same data
PostPosted: Mon Mar 10, 2008 11:00 pm     Reply with quote

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 Smile

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 ?
Ttelmah
Guest







PostPosted: Tue Mar 11, 2008 3:29 am     Reply with quote

Define both structures.
Create a union between the two.

Best Wishes
SimpleAsPossible



Joined: 19 Jun 2004
Posts: 21

View user's profile Send private message

PostPosted: Mon Mar 17, 2008 10:32 pm     Reply with quote

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.
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

Re: Using two different stucts for the same data
PostPosted: Tue Mar 18, 2008 11:04 am     Reply with quote

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 Smile

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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