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

Using switch With Strings

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



Joined: 05 Jul 2010
Posts: 129

View user's profile Send private message

Using switch With Strings
PostPosted: Tue Mar 01, 2011 3:29 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Tue Mar 01, 2011 7:30 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Mar 01, 2011 8:11 am     Reply with 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.

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

View user's profile Send private message

PostPosted: Tue Mar 01, 2011 4:42 pm     Reply with quote

How confusing! Thanks for the comments I'm back up to speed,

Cheers,
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Mar 01, 2011 5:12 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 02, 2011 2:51 am     Reply with quote

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.
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