C语言 错误:控制可能到达 C 中非空函数的结尾

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22410538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:53:16  来源:igfitidea点击:

Error: control may reach end of non-void function in C

ccontrols

提问by cspearsall

I cannot figure out why this error is happening: error: control may reach end of non-void function

我无法弄清楚为什么会发生此错误: error: control may reach end of non-void function

Here is the code:

这是代码:

bool search(int value, int values[], int n) {

    if (n < 1) {
        return false;
    }   

    for (int i = 0; i < n; i++) {
        if (values[i] == value) {
            return true;
            break;
        }
        else { 
            return false;
        }
    }    
}

I understand that the error means that the function may reach the end without returning anything, but I cannot figure out how that might happen.

我知道该错误意味着该函数可能会在不返回任何内容的情况下到达终点,但我无法弄清楚这可能是如何发生的。

回答by Grijesh Chauhan

You are getting this error because if your forloop breaks due to breaking condition i < n;then it don't find any returnstatement after forloop (see the below, I mentioned in code as comment).

您收到此错误是因为如果您的for循环因中断条件而中断,i < n;则它returnfor循环后找不到任何语句(请参阅下面,我在代码中作为注释提到)。

for (int i = 0; i < n; i++){
    if (values[i] == value){
        return true;
        break;
    }
    else{ 
        return false;
    }
}
  // here you should add either return true or false     
}

If forloop break due to i >= nthen control comes to the position where I commented and there is no returnstatement present. Hence you are getting an error "reach end of non-void function in C".

如果for由于循环中断,i >= n那么控制会到达我评论的位置,并且不return存在任何语句。因此,您收到错误“到达 C 中非空函数的结尾”。

Additionally, remove breakafter returnstatement. if returnexecutes then break never get chance to execute and break loop.

此外,删除breakafterreturn语句。如果return执行,则中断永远不会有机会执行和中断循环。

   return true;  -- it returns from here. 
    break;  -- " remove it it can't executes after return "

Check your compiler should give you a warning - 'unreachable code'.

检查你的编译器应该给你一个警告——“无法访问的代码”。

回答by HolyBlackCat

That compiler warning is not correct. Anyway, there is a bigger problem with your code:

该编译器警告不正确。无论如何,您的代码存在更大的问题:

bool search(int value, int values[], int n) {

    if (n < 1) {
        return false;
    }   

    for (int i = 0; i < n; i++) {
        if (values[i] == value) {
            return true;
            break;
        }
        else {            // !
            return false; // ! <-- Here is the mistake.
        }                 // !
    }    
}

This code only checks values[0] == valueand then always returns. It's happening because of that else {return false;}.

此代码仅检查values[0] == value然后始终返回。正因为如此else {return false;}

You should write it this way:

你应该这样写:

bool search(int value, int values[], int n) {

    if (n < 1) {
        return false;
    }   

    for (int i = 0; i < n; i++) {
        if (values[i] == value) {
            return true;
            // break;  <- BTW, it's redundant.
        }
    }    
    return false;
}

Now, function checks entire valuesarray and then returns falseif there was no matches. But if it found a match, it will instantly return truewitout checking other elements.

现在,函数检查整个values数组,false如果没有匹配项,则返回。但是如果找到匹配项,它会立即返回true而不检查其他元素。

Also, compiler will not emit a warning for this code.

此外,编译器不会为此代码发出警告。

回答by gnasher729

Your code is equivalent to

你的代码相当于

return (n > 0 && values [0] == value);

Either you are in the habit of writing very simple things in an excessively complicated way, or that code doesn't do what you want it to do.

要么您习惯于以过于复杂的方式编写非常简单的东西,要么该代码没有按照您的意愿行事。

回答by Jiminion

Some folks will probably hate this, but....

有些人可能会讨厌这个,但是......

bool search(int value, int values[], int n) {

   if (n < 1) {
      return false;
   }   

   bool ret = false;
   for (int i = 0; i < n; i++) {
      if (values[i] == value) {
         ret = true;
         break;
      }
   }    
   return ret;
}