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) { ..函数体放在这里.. }

    位于源文件的顶部。

  • 如果要在其他 C 文件中使用 point_forward(),请添加前向声明:

    void point_forward(mem_ptr m);
    

    在头文件中供其他文件包含。

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

相关文章:

C 编程 - 使用管道处理 stdout 和 stdin

c++ - window OS下c中UDP包的源IP

c - pthread_cond_wait() 的解锁和等待的原子性?

c++ - BaseAddres 从 0 开始的 VirtualQueryEx

c - 在 x64 上带有调试符号的 32 位 libc

c - 如何从c中的图像获取流

c - TCP 套接字接收指示 "unexpected"成功发送后断开连接

c++ - 为什么在 Solaris 10 中,timer_create 会抛出 SIGEV_THREAD 错误?

objective-c - 来自 NSString 的 Char(十六进制表示)

c - HANDLE 的尺寸是多少?