【leetCode】control reaches end of non-void function [-Werror=return-type]

来源:互联网 发布:中经商品交易中心软件 编辑:程序博客网 时间:2024/05/19 06:35

虽然有时我们在一块程序里已经有return,但不是在这块代码的结尾,leetCode也会编译不通过。
所以我们也要在函数的代码块结尾也return一下,如下面这个例子,一度让我怀疑人生。。。

/*    address=num[i]%height;     采用链地址法处理哈希冲突 */#include<stdio.h>#include<stdlib.h>#include<math.h> #define height 7typedef struct HashNode{    int index;//记录在nums中的下标     int value;//记录nums[i]的值     struct HashNode *next; //下一个节点的位置 }HashNode;typedef struct heads{//  int index;    HashNode *Head; }HashHead;typedef struct HashMap{    HashHead * Hs;}HashMap;HashMap * Init_HashMap(HashMap * map);HashMap * Create_HashMap(HashMap * map,int *nums,int numsSize);void Destroy_HashMap(HashMap **map);int* twoSum(int* nums, int numsSize, int target);HashMap * Init_HashMap(HashMap * map){    //初始化链地址的头部     int i;    map=(HashMap *)malloc(sizeof(HashMap));    map->Hs=(HashHead *)malloc(sizeof(HashHead)*height);    if(map->Hs){        for(i=0;i<height;i++){            map->Hs[i].Head=NULL;        }    }    return map;}HashMap * Create_HashMap(HashMap * map,int *nums,int numsSize){//数组中的数据存入哈希表。     int mod,i;    //将各个num[i]存入哈希表中     for(i=0;i<numsSize;i++){        mod=abs(nums[i])%height;        HashNode *node=(HashNode *)malloc(sizeof(HashNode));        node->index=i;        node->value=nums[i];        node->next=map->Hs[mod].Head;//尾插法         map->Hs[mod].Head=node;    }    return map; }void Destroy_HashMap(HashMap **map){    HashNode *p;    int i;    for(i=0;i<height;i++){        if((*map)->Hs[i].Head){            p=(*map)->Hs[i].Head;            (*map)->Hs[i].Head=(*map)->Hs[i].Head->next;            free(p);        }    }    free((*map)->Hs); }int* twoSum(int* nums, int numsSize, int target) {      int aMod,j;    HashNode *p;    int *result=(int *)malloc(sizeof(int)*2);    HashMap *map;    map=Init_HashMap(map);    map=Create_HashMap(map,nums,numsSize);    for(j=0;j<numsSize;j++){        aMod=abs(target-nums[j])%height;        p=map->Hs[aMod].Head;        while(p!=NULL){            if(p->value==target-nums[j] && p->index!=j){                result[0]=j;                result[1]=p->index;                Destroy_HashMap(&map);                return result;//看这里            }else{                p=p->next;            }         }       }    return result;//这里一定也要返回,虽然永远都用不到。}int main(void){    int nums[7]={-1,-2,-3,-4,-5};    int numsSize,target;    int *arr;    scanf("%d %d",&numsSize,&target);    arr=twoSum(nums,numsSize,target);     printf("%d %d",arr[0],arr[1]);      return 0;} 
阅读全文
0 0