简体   繁体   English

如何修复 HackerRank 的“错误:控制到达非空函数 [-Werror=return-type] 的末尾”?

[英]How to fix "error: control reaches end of non-void function [-Werror=return-type]" at HackerRank?

Okay, many answers are already provided, but I couldn't really seem to find the solution.好的,已经提供了很多答案,但我似乎无法真正找到解决方案。

Our task is to find the greatest of four integers.我们的任务是找到四个整数中最大的一个。

https://www.hackerrank.com/challenges/functions-in-c/problemhttps://www.hackerrank.com/challenges/functions-in-c/problem

When I run the following code:当我运行以下代码时:

#include <stdio.h>

/*
int max_of_four(int a, int b, int c, int d)
{
    if(a>b && a>c && a>d)
    return a;
    
    else if(b>a && b>c && b>d)
    return b;
    
    else if(c>a && c>b && c>d)
    return c;
    
    else
    return d;
}
*/

int max_of_four(int a, int b, int c, int d)
{
    if(a>b)
    {
        if(a>c)
        {
            if(a>d)
            return a;
        }
    }
    
    else if(b>c)
    {
        if(b>d)
        return b;
    }
    
    else if(c>d)
    return c;
    
    else
    return d;
}


int main() {
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    int ans = max_of_four(a, b, c, d);
    printf("%d", ans);
    
    return 0;
}

I am returned with this error at HackerRank :在 HackerRank返回此错误:

Solution.c: In function ‘max_of_four’:
Solution.c:45:1: error: control reaches end of non-void function [-Werror=return-type]
 }
 ^
cc1: some warnings being treated as errors

But the above code does it's job when run on Dev-C++ 5.11 .但是上面的代码在 Dev-C++ 5.11 上运行时可以完成它的工作。

If we use the function inside the comments max_of_four , and comment out the latter one, the job gets done as well.如果我们使用注释max_of_four的函数,并注释掉后一个,工作也完成了。

If somebody could get me to know, how max_of_four can be tweaked to get the code to work aptly, I'd be grateful.如果有人能让我知道如何调整max_of_four以使代码正常工作,我将不胜感激。

And what's causing the error?导致错误的原因是什么?

Is it an error or a warning?这是错误还是警告? If so, why is a warning being treated as error?如果是这样,为什么警告被视为错误?

When a function is not void and you use a if statement to return the value, you must check that every case is really handled.当函数不是 void 并且您使用if语句返回值时,您必须检查是否真的处理了每个案例。 This is not ensured by the last else in this case because you use nested statements which don't have a else .在这种情况下,最后一个 else 无法确保这一点,因为您使用了没有else嵌套语句。

Here is a fixed version :这是一个固定版本:

int max_of_four(int a, int b, int c, int d)
{
    if(a>b)
    {
        if(a>c)
        {
            if(a>d)
            return a;
        }
    }
    
    if(b>c) //<====== I removed the else here to fix the implicit
            //        else of the previous form that was not handled
    {
        if(b>d)
        return b;
    }
    
    if(c>d) //<===== Idem
    return c;
    
    else
    return d;
}

Note however that the nested if in you function could be replaced by and which would be more compact :但是请注意,嵌套if in you 函数可以替换为and ,这会更紧凑:

int max_of_four(int a, int b, int c, int d)
{
    if (a>b && a>c && a>d) return a;
    else if(b>c && b>d) return b; // Here the else keys are kept even 
    else if(c>d) return c;        // if they are not strictly necessary
    else return d;                // because of the return
}

Finally it would also be more reusable to write :最后,编写以下代码也更具可重用性:

int max_of_two(int a, int b) { return a > b ? a : b; }

Because then you can reuse it for any number of parameters :因为这样你就可以将它重用于任意数量的参数:

int max_of_three(int a, int b, int c) { 
    return max_of_two(max_of_two(a, b), c);
}
int max_of_four(int a, int b, int c, int d) { 
    return max_of_two(max_of_two(a, b), max_of_two(c, d));
}
//And so on

@dslr has already provided with optimum answer to everything, asked above. @dslr已经为上面提出的所有问题提供了最佳答案。

For any of the coming readers, what @Carcigenicate commented can be understood from the code below:对于任何即将到来的读者, @Carcigenicate评论的内容可以从下面的代码中理解:

int max_of_four(int a, int b, int c, int d)
{
    if(a>b)
    {
        if(a>c)
        {
            if(a>d)
                return a;            
            else
                return d;
        }
        else if(c>d)
            return c;
        else
            return d;
    }
    else if(b>c)
    {
        if(b>d)
            return b;
        else
            return d;
    }
    else if(c>d)
    {
        return c;
    }
    else
        return d;
}

The code runs smoothly on HackerRank, without giving any error.代码在 HackerRank 上运行流畅,没有出现任何错误。

And this is probably what @Carcigenicate meant by 'loop over array':这可能就是@Carcigenicate所说的“循环遍历数组”的意思:

int max_of_four(int a, int b, int c, int d)
{
    int i;
    int arr[4]={a,b,c,d};
    int max=arr[0];
    for(i=1;i<4;i++)
    {
        if(max<arr[i])
            max=arr[i];
    }
    return max;
}

Thanks everyone.谢谢大家。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Switch 语句错误 **控制到达非空函数的结尾 [-Werror=return-type]** - Switch Statement Error **control reaches end of non-void function [-Werror=return-type]** 错误:控制到达非无效 function [-Werror=return-type]| - error: control reaches end of non-void function [-Werror=return-type]| 为什么我收到此编译 r 错误:控制到达非无效 function [-Werror=return-type] 的结尾 - Why I am getting this Compilation r error: control reaches end of non-void function [-Werror=return-type] 如何修复“错误:控制到达非空函数结束”? - How to fix "error: control reaches end of non-void function"? C,错误:控制到达非无效函数的结尾[-Werror,-Wreturn-type] - C, error: control reaches end of non-void function [-Werror,-Wreturn-type] C 错误:非无效 function 不在所有控制路径中返回值 [-Werror,-Wreturn-type] - C error: non-void function does not return a value in all control paths [-Werror,-Wreturn-type] 编译错误 - 控制在 C 中到达非无效 function 的末尾 - Compiling error - Control reaches end of non-void function in C 初学者C:错误:控制到达非空函数的结尾? - Beginner C: error: control reaches end of non-void function? 控制到达非空函数错误的结尾 - control reaches end of non-void function error 控制到达非空 function 的末尾。 如何解决这个问题? - control reaches end of non-void function. How to solve this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM