View previous topic :: View next topic |
Author |
Message |
Geps
Joined: 05 Jul 2010 Posts: 129
|
Using switch With Strings |
Posted: Tue Mar 01, 2011 3:29 am |
|
|
Hi,
I have some code as follows:
Code: | switch (CommandForProcessing) {
case "ABCDEFG": |
Under 4.116 it compiles, under 4.087 it doesn't, citing that the string must evaluate to a constant.
Is it valid C? And is there a workaround? Maybe a char by char comparison.....?
Cheers, |
|
|
andrewg
Joined: 17 Aug 2005 Posts: 316 Location: Perth, Western Australia
|
|
Posted: Tue Mar 01, 2011 7:30 am |
|
|
That isn't valid C, or at least what will be happening is the switch will be comparing pointers and *not* what is being pointed to!
The easiest solution is an strcmp for each command. Fancier solutions involve using a character match count for each command - basically your own strcmp but character-by-character as the characters are received. _________________ Andrew |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Mar 01, 2011 8:11 am |
|
|
It is not valid C but is valid C++.
CCS has been incorporating in to the compiler C++ capabilities which is why it works in the later version but not the earlier one.
As stated, the C way and proberbly the solution to your problem is to use strcmp.
if (strcmp(buf, "MYVAL") == 0) ...
Again, initially CCS didn't allow constants in the strcmp function but this has changed. You may need to use a #device PASS_STRINGS=IN_RAM to make it work though. |
|
|
Geps
Joined: 05 Jul 2010 Posts: 129
|
|
Posted: Tue Mar 01, 2011 4:42 pm |
|
|
How confusing! Thanks for the comments I'm back up to speed,
Cheers, |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Tue Mar 01, 2011 5:12 pm |
|
|
Quote: | It is not valid C but is valid C++.
CCS has been incorporating in to the compiler C++ capabilities which is why it works in the later version but not the earlier one. |
If you had asked me, if CCS C implements any C++ features, I clearly rejected. Is it specified or at least mentioned somewhere? |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Mar 02, 2011 2:51 am |
|
|
As far as I know it is not documented.
Some of the features they have implemented (I think) are
Use of strings in switch and possibly if (str1 == str2)
Variable definitions anywhere in code :-
Code: |
void main ()
{
int i;
printf("%u", i);
int a;
printf("%u", a);
}
|
Overloaded functions (I believe)
Code: |
void func1(int a);
void func1(int a, int b);
|
Actually this one may be in one of the C standards.
I am going from memory and program in various languages so could be a mixup on them.
There are probably be others. |
|
|
|