Help - Search - Members - Calendar
Full Version: Switch With Constants
SourceBoost support forum > PIC and SX Embedded Programming > BoostC++ compiler programming
tom 2007
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
Reynard
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
IanM
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.
tom 2007
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
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.