c - 注意 : previous implicit declaration of ‘point_forward’ was here

标签 c

我似乎无法让这个递归函数正确编译,我也不确定为什么。 代码如下:

void point_forward (mem_ptr m) {
  mem_ptr temp;
  temp = m->next;
  if (temp->next != NULL) point_forward(temp);
  m->next = temp->next;
}

我的编译器返回这个:

mm.c:134:6: warning: conflicting types for ‘point_forward’ [enabled by default]
mm.c:96:2: note: previous implicit declaration of ‘point_forward’ was here

最佳答案

关键在于:

previous implicit declaration of ‘point_forward’ was here

在第 96 行你有:

point_forward(m); // where m is a mem_ptr;

由于编译器还没有看到 point_forward(m) 的函数声明,它“隐式定义”(即假设)一个返回 int 的函数:

int point_forward(mem_ptr m);

这与后面的定义冲突:

void point_forward (mem_ptr m) {

要解决此问题,您可以:

  1. 在第 96 行之前的某处显式声明:void point_forward(mem_ptr m); 这将告诉编译器如何处理 point_forward() 当它看到它在第 96 行,即使它还没有看到函数实现。

  2. 或者,在第 96 行以上定义整个函数(将函数定义从第 134 行向前移动到第 96 行以上)。

这里有点more about declaring functions .

通常,对于样式,我会:

  • 如果您不想在任何其他 C 文件中使用 point_forward(),请完整定义它:

    static void point_forward(mem_ptr m) { ..function body goes here..}

    在源文件的顶部。

  • 如果你想在其他C文件中使用point_forward(),提出前向声明:

    void point_forward(mem_ptr m);
    

    在要包含的其他文件的头文件中。

关于c - 注意 : previous implicit declaration of ‘point_forward’ was here,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16182115/

相关文章:

c - 将结构传递给 c 中的函数时出错

创建一个接受枚举变量并返回字符串指针的函数

C++程序返回int类型,为什么return -1返回255呢?

c - 在 C 中完成程序后,如何防止控制台关闭?

C:`打开调试信息

c - 微 Controller 编程

c - 没有keepalive的物理断开的tcp套接字的生命周期

c - 使二维数组和一维数组的地址相等

c - 如何在 C 中设置环境变量并启动进程?

c - 指向指针 malloc 和操作的指针