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

array of characters in switch statement

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



Joined: 06 Sep 2013
Posts: 2

View user's profile Send private message

array of characters in switch statement
PostPosted: Fri Sep 06, 2013 12:32 am     Reply with quote

hi
i am trying to use Switch statement on an array of characters so i examine a string after compiling there is no errors, but it doesn't work


Code:

UNSIGNED CHAR FUNC[]={'F','D','L'};
SWITCH(FUNC[0],FUNC[1],FUNC[2])
              {
                CASE "DDL": WORD_DISP(COUNTER_DOWN);BREAK;
                CASE "ODL": WORD_DISP(ON_DELAY);  BREAK;
                CASE "FDL": WORD_DISP(OFF_DELAY); BREAK;
                CASE "UDL": WORD_DISP(COUNTER_UP);BREAK;
                CASE "PDL": WORD_DISP(PULSE_DISP);BREAK;
                DEFAULT : BREAK;
              }
Ttelmah



Joined: 11 Mar 2010
Posts: 19394

View user's profile Send private message

PostPosted: Fri Sep 06, 2013 12:53 am     Reply with quote

Of course it won't...

That is not a C ability. In C, a 'string' is _not_ a fundamental type. It is an array of characters. This is why to compare strings you can't just say "AAA"=="BBB". You have to perform a string comparison.
Now the 'case' test 'CASE "DDL"', as coded, will take the int that 'DDL' codes as (the ASCII value of 'D'), so there is no 'syntax' error, but it is not going to work.

String comparison's in a switch, are a feature used in many other languages, but not C (not even C++).

You'd have to code this as your own 'array' search using strcmp. A basic C textbook will cover this.

There are some other 'trick' methods that could be used (encoding a string as a number).

Best Wishes
ali_kh589



Joined: 06 Sep 2013
Posts: 2

View user's profile Send private message

THANKS
PostPosted: Fri Sep 06, 2013 12:58 am     Reply with quote

ACTUALLY THIS IS THE RESULT I'VE GOT... BUT I HAVE TO MAKE SURE IF THAT... THANKS FOR YOUR INFO.
oxo



Joined: 13 Nov 2012
Posts: 219
Location: France

View user's profile Send private message

PostPosted: Fri Sep 06, 2013 2:11 am     Reply with quote

You might want to get that caps lock key fixed too.
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