#include<stdio.h>
int main()
{
int a=1;
switch(a)
{ int b=20;
case 1: printf("b is %d\n",b);
break;
default:printf("%d\n",b);
break;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在gcc 4.6.3上运行,输出不是20.这里发生了什么?
在switch-case语句中,声明初始化无效,但允许声明然后赋值.如下面的代码片段所示.
这两种类型的初始化与编译器端有什么区别?为什么第一种初始化无效,第二种类型有效.
switch(val)
{
case 0:
int newVal = 42; //Invalid
break;
case 1:
int newVal2; //Valid
newVal2 = 42;
break;
case 2:
break;
}
Run Code Online (Sandbox Code Playgroud) 我正在用C语言编写一个brainfuck解释器,我在使用一些我不习惯的东西时遇到了一些麻烦.在brainfuck中,逗号(,)本质上是getchar().所以我有以下代码:
//This is just ptr
static char *ptr;
switch (command)
{
case ',':
*ptr=getchar(); // Here's the code causing error
break;
}
Run Code Online (Sandbox Code Playgroud)
error: a label can only be part of a statement and a declaration is not a statement当我尝试编译时,gcc会抛向我.
有任何想法吗?(对不起,不太熟悉这个错误)
这是代码,当我添加其他案例或deafult时,我得到了几个错误.我找不到任何基本错误,如缺少分号左右,当我只有一个案例时代码正常工作.我通过切换教程搜索,但我没有发现任何关于向量和切换语句混合是一个问题.
int main()
{
int r;
while (cin >> r)
{
switch (r)
{
case 3:
int y = 0;
cout << "Please enter some numbers, and I will put them in order." << endl;
vector<int> nums;
int x;
while(cin >> x)
{
nums.push_back(x);
y++;
}
sort(nums.begin(), nums.end());
int z = 0;
while(z < y)
{
cout << nums[z] << ", ";
z++;
if(z > 23)
cout << "\n" << "User... What r u doin... User... STAHP!" << endl;
} …Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
void main()
{
int a = 4;
switch (a)
{
case 4:
int res = 1;
printf("%d",res);
break;
}
}
Run Code Online (Sandbox Code Playgroud)
当我用 gcc 编译这段代码时,我得到了错误
root@ubuntu:/home/ubuntu# gcc test.c -o t
test.c: In function ‘main’:
test.c:9:4: error: a label can only be part of a statement and a declaration is not a statement
int res = 1;
Run Code Online (Sandbox Code Playgroud)
但是当我添加时;,case 4:;我可以编译我的代码。
有什么问题,为什么要;解决这个问题?
考虑以下代码:
switch ("")
{
case "":
using var s = new MemoryStream();
break;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码不会编译并显示以下错误:“A using 变量不能直接在 switch 部分中使用(考虑使用大括号)”。
修复已经在建议中,但我的问题是为什么下面的代码是合法的,但上面的代码不是?为什么 C# 不能只允许前面的代码?
switch ("")
{
case "":
{
using var s = new MemoryStream();
}
// break can be inside or outside the braces
break;
}
Run Code Online (Sandbox Code Playgroud) 编译下面的代码时出现编译错误.
#include <stdio.h>
main()
{
printf("Hello 123\n");
goto lab;
printf("Bye\n");
lab: int a = 10;
printf("%d\n",a);
}
Run Code Online (Sandbox Code Playgroud)
当我编译这段代码时,它正在给予
test.c:8: error: a label can only be part of a statement and a declaration is not a statement
Run Code Online (Sandbox Code Playgroud)
为什么标签的第一部分应该是声明,为什么不是声明呢?
我们用来表示带符号的指针*.迭代该过程,我们得到一个"双"指针**,一个"三"指针***,更一般地说,一个"d维"指针(对于每个d正自然数).
问题:给定广告作为输入,将S定义为d维指针.
因为我几天前才开始研究动态结构,不幸的是我对这个问题有点麻烦.有没有人有建议/提示?
提前谢谢你,我为我可能太基本的问题道歉.
Ps:我使用了"指针"这个词而没有指明它的类型只是为了简洁起见.
在我的 ARC iOS 项目中使用 goto 时,我遇到了这个编译器错误。
无法从此 goto 语句跳转到其标签。跳转绕过保留变量的初始化
我知道 goto 总体来说很糟糕,但是......请告诉我如何解决它。代码如下,
//some process
NSArray *current = ... ;
if (current.count ==0) goto cleanup;
//proceed to next
if (processed failed) goto cleanup;
//further process
cleanup:
//clean up codes
Run Code Online (Sandbox Code Playgroud) 考虑这个C代码,其中foo是int:
switch(foo){
case 1: {
int bla = 255;
case 2:
printf("case12 %d\n", bla);
break;
case 3:
printf("case3 %d\n", bla);
}
};
Run Code Online (Sandbox Code Playgroud)
对于不同foo的代码值,给出以下输出:
case12 255 # foo=1
case12 255 # foo=2
case3 0 # foo=3
Run Code Online (Sandbox Code Playgroud)
我有一个问题的理解foo=3.声明bla和定义其值的行不应该在何时执行foo=3.switch语句应该直接跳转到标签case 3:.然而,没有警告,所以bla似乎至少已经宣布.它可能是未初始化的,但它的价值恰好是0.你能解释一下,"案例3"中发生了什么,以及为什么这是合法的C代码?