We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
u8 Key; u8 VarItem; switch (Key) { case Kb_0 : ... case Kb_1 : ... case Kb_2 : ... case Kb_3 : ... case Kb_4 : ... case Kb_5 : ... case Kb_6 : ... case Kb_7 : ... case Kb_8 : ... case Kb_9 : ... case Kb_Min : ... case Kb_Enter : switch (VarItem) { case 0 : ... case 1 : ... case 2 : ... } }
When I compile above I got one warning warning: #516-D: transfer of control bypasses initialisation off: in the line near switch(VarItem). What is the meaning of this warning. When I ignore it, all works, but I do not like warnings ...
Perhaps a default case is advised?
May be the compiler sees variable VarItem as not initialized
You showed too little code.
One of your case alternatives assigns a value to one of your variables, and the compiler can not know if this is a state machine that is guaranteed to pass this specific state.
Thanks for response.
Problem solved ! but I do not see any reason to generate a warning. The source code is a C++ file !
Following code gives a warning.
void Test(u8 num) { switch (num) { case 1 : long test = 12; break; case 2 : break; } }
following code does not.
void Test(u8 num) { long test; switch (num) { case 1 : test = 12; break; case 2 : break; } }
It seems that when you declare a variable in a switch, the compiler generates a warning. I do not see why, but however, the problem is solved ...
Luc Vercruysse
"I do not see any reason to generate a warning. The source code is a C++ file!"
Lots of C++ source files generate warnings! It's up to you to fix your source code!
A warning seems absolutely in order here! in case 1, you both define and initialise 'test', but in case 2, you skip both the definition and the initialisation - what do you expect the compiler to make of this?!
The compiler warnings describes exactly what you are doing: "transfer of control" = the jump to case 2; "bypasses initialisation" - well, obviously the initialisation at case 1 is bypassed when the execution jumps straight to case 2, isn't it?!
Correct ! Shame on me ....
Luc
"... I do not see why ..."
A new definition can occur within a brace-enclosed block:
void Test(u8 num) { switch (num) { case 1: { long test = 12; break; } case 2: break; } }