- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
case 1:
{ //question is about this curly brace
int val;
scanf("%d", &val);
if(top1 == NULL){
enqueue(top1, val, bottom1);
}
else{
enqueue(top1, val);
}
break;
}
案例 1 后没有花括号:它给出了一个错误:*
a label can only be part of a statement and a declaration is not a statement: int val;
*
最佳答案
C 文法就是这样定义的。变量声明不被视为语句:
int x; //a declaration
int y = 3; //another declaration
x = y + 1; //a statement
标签后面必须有声明。不允许标签加声明。
foo: int x; //error
bar: x = y +1; //ok
也就是IMO,不方便:
while (...)
{
//...
goto end;
end: //error, no statement
}
记住 case
只是一种特殊的标签,所以:
case 1: int x; //error
case 2: x = y +1; //ok
大括号的问题在于,在 C 语言中,它们用于构建复合语句,这当然是一种语句。复合语句里面的行既可以是声明也可以是语句,都是混合的(旧的C版本只允许在开头声明,不能在中间声明)。所以:
case 1: int x; //error: label plus declaration
case 2: { int x; } //ok: label plus compound statement
作为脚注,由于现代 C 允许混合声明和语句,您还可以编写:
case 1:; int x; //ok: label plus empty statement.
因为孤立的 ;
是一个空语句,所以它可以用于满足需要无操作语句的语法。
是使用 ;
还是 { ... }
是一个可读性问题。在 end:
示例中,我将使用 ;
,但在 case:
中,我更喜欢 {...}/
.
while (...)
{
//...
goto end;
end:; //ok, empty statement
}
switch (...)
{
case 1: //ok, compound statement
{
int x;
}
}
当然可以写出更有创意的方案,比如:
case 1: {} int x; //ok, label plus empty compound statement
关于c - 为什么我们不能在不使用花括号的情况下在 switch case 冒号后声明变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47970812/
我正在尝试使用字符串类型的变量动态调用函数,即变量将包含字符串形式的函数名称。因此,我需要使用该变量调用函数。 所以,我正在使用 Scala Reflection。如果函数接受 Sting 数据类型,
class X extends Y { X(int a, int b) : super(a,b); } 有人可以给我解释冒号:的语法含义吗? 最佳答案 Dart 中的此功能称为“初始化列表”。 它
我正在寻找一些为旧的 jQuery 版本 v.1.6.4 编写的 jquery 代码。它有一个多属性选择器,如下所示: "span:[id^='someid-']" 选择器用于各种功能,如“查找”
This question already has answers here: How do you access the matched groups in a JavaScript regular
显然,冒号在 Java 中有多种使用方式。有人介意解释一下它的作用吗? 例如这里: String cardString = ""; for (PlayingCard c : this.list) /
我在用 c 编程,我将 c 代码编译为汇编代码,但是当我用 NASM 汇编程序重新编译代码时,它给我一个错误 操作数后预期的逗号、冒号、修饰符或行尾。这发生在 第 6 行,第 7 行和第 8 行。请帮
我正在寻找一个字符来使用文件名定界符(我将多个文件名存储在一个纯文本字符串中)。 Windows 似乎不允许 : , ? , * , , " , | , /和 \在文件名中。显然,\和 /不能使用,
我有一个变量 $entry 设置为: stdClass Object ( [im:name] => stdClass Object ( [label] => Amazing Breaker ) etc
这个问题不太可能对任何 future 的访客有帮助;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于互联网的全局受众。如需帮助使这个问题更广泛适用,visit the h
本文实例讲述了Python数据类型中的“冒号“[::]——分片与步长操作。分享给大家供大家参考,具体如下: 例如有如下字符串: ?
我创建了一个将 Excel 文件转换为 Access 数据库的应用程序。在转换过程中,MACAddress 列数据中的“:”符号需要替换为空格。 我尝试使用 replace 方法修改查询,但它不起作用
我在 ruby 中将散列转换为 yaml,但它添加了 : 到键:例如: :name: "Name" :value: "Value" 如果有办法避免并输出: name: "Name" value:
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicate: VB Using colons to put two statements on same line 我在 Exce
我的 createSQLQuery() 方法使用具有 : 字符的表 Tables::IMS_EPISODE, 这将被视为a Paramater ,需要设置 有没有办法转义这个字符,这样它就不会被视为参
我收到了一个定义游戏“棋盘”对象的 Python 类,以及一些在构造函数中初始化的成员。大部分内容都很简单,但我不理解此代码段最后两行中集合初始化中“:”运算符的含义: class Board: BL
在我的元素中,有一个库生成元素的情况,我需要从那里选择特定的元素——恰好包含一个带有“:”的属性。 换句话说,我最终尝试选择使用:document.querySelectorAll("[xml:spa
这个问题在这里已经有了答案: Is there any difference between the `:key => "value"` and `key: "value"` hash notati
这个问题在这里已经有了答案: What does C++ struct syntax "a : b" mean (5 个回答) 关闭8年前。 struct _USBCHECK_FLAGS {
我在分配变量时不小心使用了 : 而不是 = ,令我惊讶的是它没有产生错误。例如,以下运行没有投诉: Python 3.7.4 (default, Jul 9 2019, 18:15:00) [Cla
我对定义/调用 Lua 函数时使用的冒号表示法感到非常困惑。 我以为我已经明白了,直到我看到这段代码: function string.PatternSafe( str ) return (
我是一名优秀的程序员,十分优秀!