簡體   English   中英

程序在現場但在代碼塊上沒有錯誤

[英]error in program on site but not on codeblocks

我正在進行競爭編碼,並且在站點(即黑客)上,我的程序給出了“錯誤控制到達非空函數[-Werror = return-type]的末尾”的信息,但是在代碼塊上,這很好,這是我的代碼。

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int count_max_freq(int *a,int n,int i)
{   static int max_freq=0,index;
    int t=a[i],f=0;
    for(int j=i;j<n;j++)
        if(a[i]==a[j])
            f++;
    if(max_freq<f)
    {   max_freq=f;
        index=i;
    }
    if(i<n)
        count_max_freq(a,n,i+1);
    else
        return a[index];
}
int main(){
    int n;
    scanf("%d",&n);
    int *types = malloc(sizeof(int) * n);
    for(int types_i = 0; types_i < n; types_i++){
       scanf("%d",&types[types_i]);
    }
    // your code goes here
    printf("%d",count_max_freq(types,n,0));
    return 0;
}

這些返回路徑之一不會返回任何內容(遞歸路徑),因此會發出警告。

它起作用的事實僅僅是運氣(不確定您所說的是什么:“它可以編譯”並不意味着“它可以正常運行”,完全可以靠運氣,但我不會打賭)

我的建議:替換為:

if(i<n)
    count_max_freq(a,n,i+1);  // should return the value!
else
    return a[index];

用三元表達式表示,因此您只有一個return語句,沒有警告,並且可以在任何地方使用:

return (i<n) ? count_max_freq(a,n,i+1) : a[index];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM