- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
写一个简单的评估我遇到了一个有趣的问题。
给定代码:
enum node_type {LEAF, NODE};
struct tree_elm_t {
enum node_type type;
union {
struct tree_node_t node;
struct tree_leaf_t leaf;
} datum;
};
int parse_leaf(struct tree_leaf_t leaf);
int parse_node( struct tree_node_t node );
int parse_tree( struct tree_elm_t* tree );
....
int parse_tree( struct tree_elm_t* tree ) {
switch( tree->type ) {
case NODE: return parse_node(tree->datum.node);
case LEAF: return parse_leaf(tree->datum.leaf);
}
}
我很惊讶地看到 gcc 提示缺少控制流选项:
example.c: In function 'parse_tree':
example.c:54: warning: control reaches end of non-void function
流问题可以通过将返回值存储在变量中来解决,如下所示:
int parse_tree( struct tree_elm_t* tree ) {
int sum;
switch( tree->type ) {
case NODE: sum = parse_node(tree->datum.node); break;
case LEAF: sum = parse_leaf(tree->datum.leaf); break;
}
return sum;
}
不过我确实发现原始代码更清晰,有没有办法让 gcc 接受原始代码 -(我想进行静态分析以了解我的代码是有效且干净的)。
编辑:
我可能有点不清楚。
假设我编译了以下代码:
int parse_tree( struct tree_elm_t* tree ) {
int sum;
switch( tree->type ) {
case NODE: sum = parse_node(tree->datum.node); break;
// case LEAF: sum = parse_leaf(tree->datum.leaf); break;
}
return sum;
}
gcc 会给我一个警告:
example.c: In function 'parse_tree':
example.c:51: warning: enumeration value 'LEAF' not handled in switch
这意味着 gcc 了解开关中值的选项,以及我已经注释掉 LEAF 案例的事实。这意味着 gcc 也知道在通过 switch 时会检查每个案例。那么为什么声明:
control reaches end of non-void function
它是否缺少 gcc 中缺少的静态分析系统 - 或语言功能?
最佳答案
你的编译器在提示,因为你函数逻辑中的所有路径都应该返回一个值(正如这个函数的原型(prototype)所规定的):
int parse_tree( struct tree_elm_t* tree ) {
switch( tree->type ) {
case NODE: return parse_node(tree->datum.node);
case LEAF: return parse_leaf(tree->datum.leaf);
default: return 0; // <-- problem solved
}
}
编译器(就像我在这个答案中一样)更关注代码的语法而不是语义。
虽然您已经定义了 enum node_type {LEAF, NODE}
,但您的编译器不想依赖此约束并接受 type
的可能性 tree->type
语句具有与 NODE
或 LEAF
不同的值。
编辑:我试过这段代码:
enum node_type {LEAF, NODE};
struct node { enum node_type type; };
int parse_tree( struct node* n ) {
switch( n->type ) {
case NODE: return 1;
case LEAF: return 2;
}
}
int main() {
struct node n;
printf("%d", parse_tree(&n));
return 0;
}
在ideone上,结果如下:
(gcc-4.8.1,编译为“C”)~ http://ideone.com/b0wdSk : 代码有效,输出 2
(gcc-4.8.1,编译为“C++”)~ http://ideone.com/OPH5Ar : 与“C”相同
(gcc-4.8.1,编译为“C99 strict”)~ http://ideone.com/ou71fe : 无效因为:
error: control reaches end of non-void function [-Werror=return-type]
并支持Martin Kristiansen关于为枚举分配任何整数值的观点是有效的,我已经尝试过 struct node n; n.type = 7;
使用相同的代码和“C”以及“C99 strict”,编译器根本不会提示。但是“C++”给出:
error: invalid conversion from ‘int’ to ‘node_type’ [-fpermissive]
关于c - 在为所有枚举值定义大小写后,编译器仍然说 : "control reaches end of non-void function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18680378/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预
出于某种原因,右栏中的精选文章忽略了“#elementtext”和“#elementtext:hover”属性。仅显示“p.element”和“img.element”。 有什么想法吗? 谢谢 - 塔
我有两个值,每个值都来自不同的枚举。我想检查这两者的允许组合,如果没有找到则执行默认操作。我能以某种方式对这两个值进行切换/大小写吗?我想避免使用多个 if/else 语句或遵循位掩码模式的枚举,只是
我需要 where 但 not 大小写。例如,我想找到没有名字“莎士比亚”的戏剧: _.where(listOfPlays, {author: !"Shakespeare", year: 1611})
我想实现一个 parking 场应用所以有一个带5个或更多 parking 位的车库当司机 parking 时,车库中的下一个空闲位置应该分配给他。 所以我有一个带 5 个或更多插槽的 table 上
我想使用 Erlang 来确定传递给函数的变量是否可以被数字整除。我考虑过使用 case 来执行此操作,但是我找不到解决方案。 case 是适合这项工作的工具吗? 示例:将数字传递给函数 f()。如果
我在 phpmyadmin 中创建了一个表,其列名如 first_name、last_name。当我使用命令显示表中的列名时,它会将它们显示为 first_name。 我想显示我的列名称,如 Firs
使用 Swift 4,如何使用这些规则格式化字符串: 如果单词超过 3 个字母,则首字母大写,否则大写 包含像 St-Michel 这样的连字符的 Pascal 大小写单词 我这里有初稿,但我一直在思
这个问题在这里已经有了答案: Why can't the switch statement be applied to strings? (23 个回答) 关闭 8 年前。 大家好 所以我正在尝试对
在 MVC 操作中,我如何访问使用多个同名值提交的“表单数据”中的值? 我做了什么:int、decimal、string 类型的值工作完美。 问题:每个变体都有一个复选框,所以当我尝试获取它时,它只显
while(1) { char buff[1000]; printf("Enter the word: "); fgets(buff, 1000
我有一个 Dllmain,它在线程附加到此 DLL 时分配线程本地存储。代码如下: BOOL APIENTRY DllMain(HMODULE hModule,
我有一个变量名,比如“WARD_VS_VITAL_SIGNS”,我想将它转换为 Pascal 大小写格式:“WardVsVitalSigns” WARD_VS_VITAL_SIGNS -> WardV
我是 Swift 编码的新手,正在尝试弄清楚如何在触摸节点时制作具有开/关功能的循环音频。我认为实现它的最佳方式是通过 SKAudioNode,但我不确定我在以下代码中做错了什么。当在节点上按下时 -
这是我第一次使用这种枚举,具有关联值类型的枚举,我需要根据对象的类型制作一个 switch 语句,我无法做到,这是枚举: enum TypeEnum { case foo(FooClass)
我想从字符串中删除所有下划线,下划线后面的字符为大写。因此,例如:_my_string_ 变为:MyString 同样:my_string 变为 MyString 有没有更简单的方法呢?我目前有以下内
如何在 Java 中将蛇形大小写转换为 Camel 形大小写? 输入:“input_in_snake_case” 输出:“InputInSnakeCase” 最佳答案 Guava通过其CaseForm
我们有一个表auth_group_access,那么如何使用呢? 在使用M方法时,对于带下划线的表名,可以采用如下方法。 M('AuthGroupAccess'); 对应sql语句SQL: S
我正在制作一个 pygame 游戏,每当我运行我的代码时,我都会收到错误 expected ':'。我知道在 match/case block 中使用 [ 和 ] 用于其他用途,但我该如何解决这个问题
有人能告诉我是否可以使用正则表达式将 url 转换为小写? 这是在 html img 标签内,所以我们可以通过标签找到网址。 这是我所拥有的一个例子 我需要在最后小写图像名称。 该文档包含更多 H
我是一名优秀的程序员,十分优秀!