获取“功能冲突类型"在 C 中,为什么? [英] Getting "conflicting types for function" in C, why?

查看:22
本文介绍了获取“功能冲突类型"在 C 中,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码:

char dest[5];
char src[5] = "test";

printf("String: %s\n", do_something(dest, src));

char *do_something(char *dest, const char *src)
{
    return dest;
}

do_something 的实现在这里并不重要.当我尝试编译上面的代码时,我得到了这两个异常:

The implementation of do_something is not important here. When I try to compile the above I get these two exception:

错误:'do_something' 的类型冲突(在 printf 调用中)
错误:'do_something' 的先前隐式声明在这里(在原型行)

error: conflicting types for 'do_something' (at the printf call)
error: previous implicit declaration of 'do_something' was here (at the prototype line)

为什么?

推荐答案

您试图在声明之前调用 do_something.您需要在 printf 行之前添加一个函数原型:

You are trying to call do_something before you declare it. You need to add a function prototype before your printf line:

char* do_something(char*, const char*);

或者您需要将函数定义移动到 printf 行上方.在声明之前不能使用函数.

Or you need to move the function definition above the printf line. You can't use a function before it is declared.

这篇关于获取“功能冲突类型"在 C 中,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆