View previous topic :: View next topic |
Author |
Message |
ali_kh589
Joined: 06 Sep 2013 Posts: 2
|
array of characters in switch statement |
Posted: Fri Sep 06, 2013 12:32 am |
|
|
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: 19589
|
|
Posted: Fri Sep 06, 2013 12:53 am |
|
|
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
|
THANKS |
Posted: Fri Sep 06, 2013 12:58 am |
|
|
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
|
|
Posted: Fri Sep 06, 2013 2:11 am |
|
|
You might want to get that caps lock key fixed too. |
|
|
|