switch 문장에서case 문장에서 변수 초기화 문제!
1510 단어 compilerinitialization
#include<stdio.h>
int main()
{
int i=1;
switch(i)
{
case 1:
int a=1;
printf("%d/n",a);
break;
case 2:
printf(" ");
break;
default:break;
}
return 0;
}
컴파일이 [error] D:/패키지/C-Free/Temp/이름이 지정되지 않았습니다. 1.cpp:11: error: jump to case label
[ERROR] D:/패키지/C-Free/Temp/이름 없음 1.cpp:8: error: crosses initialization of `int a'
솔루션 1:
#include<stdio.h>
int main()
{
int i=1;
switch(i)
{
case 1:
{
int a=1;
printf("%d/n",a);
}
break;
case 2:
printf(" ");
break;
default:break;
}
return 0;
}
시나리오 2:
#include<stdio.h>
void f();
int main()
{
int i=1;
switch(i)
{
case 1:
f();
break;
case 2:
printf(" ");
break;
default:break;
}
return 0;
}
void f()
{
int a=1;
printf("%d/n",a);
}
원인: for int a = 1;the compiler knows Arnald needs a integer a with 1 as its initial value. And it also found it is possible that in default region, the a can be used. Since the compiler cannot make sure the a will be initialized correctly for every flag, then it just issue an error(warning for some other compilers), and hope you can handle it...
And if you remove the default part, the error will dispear, since there is no initialization *skip* now...
Just put them in the case scope if you really want it...
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
matlab 컴파일러가 컴파일한 함수를 호출합니다function [x, y] = sierpinski(iterations, draw) % SIERPINSKI Calculate (optionally draw) the points in Sierpinski's trian...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.