IPB

Welcome Guest ( Log In | Register )

> Switch With Constants
tom 2007
post Nov 17 2009, 12:18 PM
Post #1


Newbrie


Group: EstablishedMember
Posts: 15
Joined: 10-August 07
Member No.: 3,739



Hello

I have a problem with the "switch":

QUOTE
char data[4]={0};
const char adr1=240;
const char adr2=241;
const char adr3=242;
const char adr4=243;


void main(){
char dev=0;

//some other code
switch(data[1]){
case adr1:
dev=0;
break;
case adr2:
dev=1;
break;
case adr3:
dev=2;
break;
case adr4:
dev=3;
break;
default:
dev=-1;
break;
}
}


the compiler says "error: non-constant argument in 'case' expression" but the adr1,2,3,4 are constants!
If i replace "const char adr1=240;" with "#define adr1 240" but then i get the error:
error: missing colon
error: missing right brace
error: missing right paren
error: failure

The code works fine if i replace the adr1,2,... with the numbers but why is it not working with constants?

Tom
update SourceBoost release 6.97 is available.
Download Now
Go to the top of the page
 
+Quote Post
 
Start new topic
Replies
tom 2007
post Nov 19 2009, 08:47 AM
Post #2


Newbrie


Group: EstablishedMember
Posts: 15
Joined: 10-August 07
Member No.: 3,739



yea there must be a typo but can't find it the line i get that "error: missing colon" on is the "case adr1:" line (when i use "#define adr1 240").

nvm i tried again today and now it works blink.gif unsure.gif
Go to the top of the page
 
+Quote Post

Posts in this topic


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



Lo-Fi Version Time is now: 3rd September 2010 - 03:01 PM