Switch statement Compiler bug? QNX 6

I’ve found that if I want to create an object within a case and initialise it then I have to put braces around the case.

The following gives a compiler error allong the lines of
: jump to case label

(the next case or default): crosses initialization of `int x’


switch ******
{
case ******:
int x = 1;
break;

case ******:
break;

}


The following does not

switch ******
{
case ******:
int x;
break;

case ******:
break;

}


nor does

switch ******
{
case ******:
{int x = 1;}
break;

case ******:
break;

}

I don’t think this is really a bug.

In original ANSI C, I think variable declaration is not allowed inside
a switch/case statement.

The fact that your ‘int x;’ does not give an error is probably
because this instruction actually does not evaluate to
anything while compiled, since x is not used within its
declaration scope.

‘int x = 1;’ on the other hand, evaluates to something.

I have always put brackets around code blocks inside ‘case’
statements whenever I wanted to declare some local variables.
Many, many compilers will require you to do so: I don’t think
you can call it a bug, just adapt to it :wink: