9 years, 5 months ago.

Does Switch case works with uint16_t

Hello!

I've been trying to use switch case in order to test an uint16_t but it is not working. I am using a FRDMKL25Z. I will explain:

I defined a value in two diferent ways.

If I use:

#define VERSION 0x4141

And in the switch case block i made the following test:

if I use

int comand = 0x4141;
 switch (comand){ //Warning: Transfer of control bypasses initialization of in "main.cpp", Line: 77, Col: 7

   
        case VERSION:

///....code

I receive the following message:

Warning: Transfer of control bypasses initialization of in "main.cpp", Line: 92, Col: 6

and if i define

#define VERSION 'AA'

I get the following message:

Warning: Multicharacter character literal (potential portability problem) in "main.cpp", Line: 90, Col: 19 :

Do you know why does it happen?

The program kind of works, but it does not test the lsb byte of the uint16_t, only the msb

Thanks!!

Version appears to be a const char and comand an int and you are trying to compare them?

posted by David Fletcher 12 Jun 2015

How do you know if that it is a const char? Because of the #define?

posted by Thiago . 13 Jun 2015

Yes! If you want a hex byte then use 0xAA, but it will still be a constant uint8/int8. Or use 0x00AA for a constant uint16/int16.

posted by David Fletcher 13 Jun 2015

If you want the value of the ASCII code for 'A' in the high and low bytes of the 16 bit value then you need to use 'A' twice and shift one of the values left. e.g.

#define version (('A' << 8) | 'A')

The other warning is probably generated by some syntax or formatting error. Switch should work fine with a uint16_t.

posted by Andy A 15 Jun 2015

1 Answer

7 years, 2 months ago.

"Warning: Transfer of control bypasses initialization" of a switch statement is certainly caused by declaring a variable in the middle of a case statement. I discovered this when I changed from an evolving if(){}else if(){}else{} etc to a switch statement and forgot I had declared some temp vars in the structure.

A rather typical totally bizarre warning that really isn't helpful... :)

Googling gave me no answer (but it brought me here), so I did what I recommend anyone do in these situations.. comment out chunks of local code until the error disappears, then halve the removals to see which half etc.