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 CCS Technical Support

typdef, struct and enums in ccsc

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



Joined: 22 Sep 2003
Posts: 52
Location: UK

View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger

typdef, struct and enums in ccsc
PostPosted: Thu Apr 07, 2005 6:20 am     Reply with quote

Hi all,

In an attempt to make my code as readable as possible for future maintenance I should like to create some enums, typedef structs and arrays of structs!!!! Shocked

Does anyone have an opinion as to how well this will work. It seems to compile OK!

Code:

typedef enum PortStatusDef
{
   Unconnected = 0,
   NewPortDetected = 1,
   Port_A = 2,
   CheckStillA = 3,
   CandidateLow = 4,
   CheckIfCOnnected = 5,
   NewPortB = 6,
   LocalCheck1 = 7,
   LocalCheck2 = 8,
   Port_B = 9,
   PortBLow = 10,
   CSLookingForRemote = 11,
   WaitPortALow = 12
};

typedef struct {
   PortStatusDef PortState;
   int8 PortNumber;
   int8 LastState;
   int8 NewState;
   int8 time;
   int8 NewStateStableTime;
} port;

port PortStatus[24];

void ProcessMessage(int8 msg, int8 thisPort)
{
   switch (PortStatus[thisPort].PortState)
   {
      case Unconnected :
      {
      // detemine what to do here based on value of msg.
      }
      case NewPortDetected :
      {
      // detemine what to do here based on value of msg.
      }
      case Port_A :
      {
      // detemine what to do here based on value of msg.
      }
      .
      .
      .
      .
      default:
         ;
   }
}


I guess I'm asking if creating a typedef enum in an attempt to only use the correct value is a good move?

Thanks in advance.

Jason.
Ttelmah
Guest







PostPosted: Thu Apr 07, 2005 3:24 pm     Reply with quote

Using enums, is a good way of helping to prevent 'silly' accidents with values. Especially dealing with a set of values like this, and a switch statement, I'd say it was a good way to go.
There is a 'caveat', that CCS, can develop some oddities if declarations get too complex, but the current versions seem better in this regard...

Best Wishes
Guest








PostPosted: Fri Apr 08, 2005 12:56 am     Reply with quote

Many thanks. Any other thoughts on typedef enum or typedef struct?

Regards.
jamesjl



Joined: 22 Sep 2003
Posts: 52
Location: UK

View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger

PostPosted: Fri Apr 08, 2005 12:57 am     Reply with quote

Sorry. The post above was from me. Just wasn't logged in.

Jason.
Guest








PostPosted: Fri Apr 08, 2005 1:18 pm     Reply with quote

I have a question. Will the "PortStatusDef PortState;" in the struct occupy 8-bits or 4-bits in the structure?
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Apr 08, 2005 1:46 pm     Reply with quote

Anonymous wrote:
I have a question. Will the "PortStatusDef PortState;" in the struct occupy 8-bits or 4-bits in the structure?


The enum in this case is an 8 bit number so 8 bits.
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