IPB

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Switch With Constants
tom 2007
post Nov 17 2009, 12:18 PM
Post #1


Newbrie


Group: EstablishedMember
Posts: 12
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
Interested in advertising here? Contact support@sourceboost.com
Go to the top of the page
 
+Quote Post
Reynard
post Nov 17 2009, 04:47 PM
Post #2


Super Enthusiast
***

Group: EstablishedMember
Posts: 406
Joined: 24-October 07
From: Scotland
Member No.: 3,861



Hi Tom,

I think the case statement requires a constant expression rather than a constant variable with const attached to a type specifier.

#define does work so you may have a typo.

You may want to look at enum for you case constants.

Cheers

Reynard
Go to the top of the page
 
+Quote Post
IanM
post Nov 18 2009, 03:23 AM
Post #3


Enthusiast
**

Group: EstablishedMember
Posts: 128
Joined: 11-June 09
From: UK
Member No.: 5,160



QUOTE (Reynard @ Nov 17 2009, 04:47 PM) *
Hi Tom,

I think the case statement requires a constant expression rather than a constant variable with const attached to a type specifier.

#define does work so you may have a typo.

You may want to look at enum for you case constants.

Cheers

Reynard


Reynard has hit the nail on the head. This use of 'const' in C++ is virtually identical to that in C. Under ANSI C 'const' is no more and no less than a promise that you do not intend to modify the variable and wish to be warned by the compiler if any code makes an obvious attempt to do so. It is even legal and often correct to declare a variable with both 'const' and 'volatile modifiers. This is usually done for read only memory mapped status registers or locations such as timers maintained by system kernal code that user programs are not permitted to modify. A const variable has an address and can be passed as a pointer. A C integer constant is very much a different thing altogether . . .

I also noticed some suspicious types. 'char' variables containing values that may exceed 127 need to be declared as 'unsigned char' as only 0 to 127 is requirred by the standard. Similarly dev needs to be 'signed char' as you assign -1 to it. Its range would then be -128 to 127. This alone would cause your program to fail if a future BoostC++ was extended to support non-constant switch cases.

Incidentally BoostC v6.96 compiles it perfectly if you use the #define form. The errors you were getting look exactly like the errors one gets if a semicolon is accidentally put at the end of each #define.
Go to the top of the page
 
+Quote Post
tom 2007
post Nov 19 2009, 08:47 AM
Post #4


Newbrie


Group: EstablishedMember
Posts: 12
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

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: 9th February 2010 - 01:47 PM