G
gyan
follwing code gives error:
1 #include<iostream.h>
2 int main()
3 {
4 int a=5,b;
5 switch(a){
6 case 1:
7 {b=5;
8 break;
9 }
10 case 2:
11 b=7;
12 int c=4;
13 c=3;
14 break;
15 case 3:
16 c=2;
17 break;
18 }
19 return 0;
20 }
"1.cpp", line 15: Error: This case bypasses initialization of a local
variable.
If i put statements under case 2: in {}, i get error:
"1.cpp", line 18: Error: c is not defined.
So exactly what is happening?
i have read that
A problem occurs , when a variable is declared AND INITIALIZED, in a
location where the flow-of-control is undetermined relative to the
location of the declaration (after the declaration has been moved to the
beginning of the scope block).
Or perhaps a better way of saying it, is the compiler recognizes the
declaration and initialization as two different things:
the declaration is moved to a "static" location at the beginning of the
scope block
while the initialization is really an assignment statement whose execution
is "dynamic", that is, it depends on the flow of control through the
program
Can someone one explain reason in a more common way. I am not able to
understand it.Can you put code, as seen by compiler.
1 #include<iostream.h>
2 int main()
3 {
4 int a=5,b;
5 switch(a){
6 case 1:
7 {b=5;
8 break;
9 }
10 case 2:
11 b=7;
12 int c=4;
13 c=3;
14 break;
15 case 3:
16 c=2;
17 break;
18 }
19 return 0;
20 }
"1.cpp", line 15: Error: This case bypasses initialization of a local
variable.
If i put statements under case 2: in {}, i get error:
"1.cpp", line 18: Error: c is not defined.
So exactly what is happening?
i have read that
A problem occurs , when a variable is declared AND INITIALIZED, in a
location where the flow-of-control is undetermined relative to the
location of the declaration (after the declaration has been moved to the
beginning of the scope block).
Or perhaps a better way of saying it, is the compiler recognizes the
declaration and initialization as two different things:
the declaration is moved to a "static" location at the beginning of the
scope block
while the initialization is really an assignment statement whose execution
is "dynamic", that is, it depends on the flow of control through the
program
Can someone one explain reason in a more common way. I am not able to
understand it.Can you put code, as seen by compiler.