R
Robbie Hatley
Greetings, group. I just found a weird problem in a program where
a variable declared in a {block} after a "case" keyword was being
treated as having value 0 even though its actual value should
have been something else. An extremely stripped-down version:
int Function (int something)
{
switch(something)
{
case WHATEVER:
{
int DumVar = 72;
// Some code uses DumVar. It seems to see the value
// of DumVar as being 0 ! What the heck???
break;
}
}
}
Is there anything wrong with that? Does it violate some rule
of C that I'm missing? It's almost as if the first time I
try to read Dumvar in the code, it's being redeclared (as int),
redefined, and reinitialized to 0.
When I moved the declaration of DumVar up to the top of the
function, it now works correctly:
int Function (int something-)
{
int DumVar = 72;
switch(something)
{
case WHATEVER:
{
// Code here now sees the value of DumVar as being 72
break;
}
}
}
So am I just missing something, or is my compiler buggy?
(I'm using National Instruments "CVI" compiler for this;
I haven't tested this on a more common compiler such as GCC.)
a variable declared in a {block} after a "case" keyword was being
treated as having value 0 even though its actual value should
have been something else. An extremely stripped-down version:
int Function (int something)
{
switch(something)
{
case WHATEVER:
{
int DumVar = 72;
// Some code uses DumVar. It seems to see the value
// of DumVar as being 0 ! What the heck???
break;
}
}
}
Is there anything wrong with that? Does it violate some rule
of C that I'm missing? It's almost as if the first time I
try to read Dumvar in the code, it's being redeclared (as int),
redefined, and reinitialized to 0.
When I moved the declaration of DumVar up to the top of the
function, it now works correctly:
int Function (int something-)
{
int DumVar = 72;
switch(something)
{
case WHATEVER:
{
// Code here now sees the value of DumVar as being 72
break;
}
}
}
So am I just missing something, or is my compiler buggy?
(I'm using National Instruments "CVI" compiler for this;
I haven't tested this on a more common compiler such as GCC.)