![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
jamesjl
Joined: 22 Sep 2003 Posts: 52 Location: UK
|
typdef, struct and enums in ccsc |
Posted: Thu Apr 07, 2005 6:20 am |
|
|
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!!!!
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. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Thu Apr 07, 2005 3:24 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Fri Apr 08, 2005 12:56 am |
|
|
Many thanks. Any other thoughts on typedef enum or typedef struct?
Regards. |
|
![](templates/subSilver/images/spacer.gif) |
jamesjl
Joined: 22 Sep 2003 Posts: 52 Location: UK
|
|
Posted: Fri Apr 08, 2005 12:57 am |
|
|
Sorry. The post above was from me. Just wasn't logged in.
Jason. |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Fri Apr 08, 2005 1:18 pm |
|
|
I have a question. Will the "PortStatusDef PortState;" in the struct occupy 8-bits or 4-bits in the structure? |
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Apr 08, 2005 1:46 pm |
|
|
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. |
|
![](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
|