c - "warning: control reaches end of non-void function"但实际上该函数被声明为 int 并返回一个 int

标签 c gcc

我有这个功能:

int firstHeapArray (IntHeapArray h) {
    if(!emptyHeapArray(h))
        return h.array[0];
}

它被声明为 int,这是 IntHeapArray 结构:

typedef struct intHeapArray {
    int *array;
    int size;
} IntHeapArray;

你能说说为什么我在编译时会收到这个警告吗?

heap-test.c: In function ‘firstHeapArray’:
heap.h:31:1: warning: control reaches end of non-void function

非常感谢。

最佳答案

错误消息表明您有一个不是 void 返回值的方法,但您在没有返回值的情况下到达终点。您需要返回一个有效的 int 值。

问题出在这里:

int firstHeapArray (IntHeapArray h) {
    if(!emptyHeapArray(h))
        return h.array[0];

    // If emptyHeapArray(h) is true, you reach here...

   // You need to return SOMETHING here to remove the warning
   return 0;
}

关于c - "warning: control reaches end of non-void function"但实际上该函数被声明为 int 并返回一个 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10964528/

相关文章:

c - 编写链表实现时出现的 C 指针问题

c - 如何让 GCC 警告函数调用和函数定义中参数数量不匹配?

c - C中数据类型混合

在 C 中检查文本文件长度

将二维数组转换为单个数组

比较C中的两个矩阵

c++ - 在生产环境中使用 PGO(profile-guided optimization)的风险

c++ - int64 C++ debian6

c++ - 在 friendlyarm qtopia 错误上编译

c++ - 数组中的函数指针(nix c++)