Jump to content
tom 2007

Switch With Constants

Recommended Posts

Hello

 

I have a problem with the "switch":

 

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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::unsure:

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...