C语言 使用gcc编译c程序时出现冲突类型错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5691650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
conflicting types error when compiling c program using gcc
提问by xiao Х
I tried to compile following program with gcc.
我试图用 gcc 编译以下程序。
0 #include <stdio.h>
1
2 main ()
3
4 {
5 char my_string[] = "hello there";
6
7 my_print (my_string);
8 my_print2 (my_string);
9}
10
11 void my_print (char *string)
12 {
13 printf ("The string is %s\n", string);
14 }
15
16 void my_print2 (char *string)
17 {
18 char *string2;
19 int size, i;
20
21 size = strlen (string);
22 string2 = (char *) malloc (size + 1);
23
24 for (i = 0; i < size; i++)
25 string2[size - i] = string[i];
26
27 string2[size+1] = 'void my_print (char *);
void my_print2 (char *);
';
28 printf ("The string printed backward is %s\n", string2);
29 }
However, it fails and the compiler produces following error log:
但是,它失败并且编译器产生以下错误日志:
- greeting.c: 11: error:conflicting types for 'my_print'
- greeting.c: 7: error: previous implicit declaration of 'my_print' was here
- greeting.c: 16: error:conflicting types for 'my_print2'
- greeting.c:8: erroro:previous implicit declaration of 'my_print2' was there
- greeting.c: 11: 错误:'my_print' 的类型冲突
- greeting.c: 7: 错误:先前隐式声明 'my_print' 在这里
- greeting.c: 16: 错误:'my_print2' 的类型冲突
- greeting.c:8: erroro: 'my_print2' 的先前隐式声明在那里
And if I move the my_print and my_print2 functions before the main function, everything goes well. So can anyone explain why the problem happens? Thanks!
如果我在 main 函数之前移动 my_print 和 my_print2 函数,一切都会顺利。那么任何人都可以解释为什么会出现问题?谢谢!
回答by MByD
If you don't declare a function and it only appears after being called, it is automatically assumed to be int, so in your case, you didn't declare
如果你没有声明一个函数并且它只在被调用后出现,它会被自动假定为int,所以在你的情况下,你没有声明
void my_print (char *);
void my_print2 (char *);
before you call it in main, so the compiler assume there are functions which their prototypes are int my_print2 (char *);and int my_print2 (char *);and you can't have two functions with the same prototype except of the return type, so you get the error of conflicting types.
你在主调用之前,所以编译器假设有功能,这它们的原型int my_print2 (char *);和int my_print2 (char *);你不能有相同的原型两个功能不同的返回类型,所以你得到的错误conflicting types。
As Brian suggested, declare those two methods before main.
正如 Brian 建议的那样,在 main 之前声明这两个方法。
回答by Brian Roach
You have to declare your functions beforemain()
您必须声明你的函数之前,main()
(or declare the function prototypes before main())
(或之前声明函数原型main())
As it is, the compiler sees my_print (my_string);in main()as a function declaration.
实际上,编译器将my_print (my_string);inmain()视为函数声明。
Move your functions above main()in the file, or put:
将您的函数移至main()文件上方,或放入:
Above main()in the file.
main()在文件上面。
回答by Vikas Goel
To answer a more generic case, this error is noticed when you pick a function name which is already used in some built in library. For e.g., select.
为了回答更通用的情况,当您选择某个内置库中已使用的函数名称时,会注意到此错误。例如,选择。
A simple method to know about it is while compiling the file, the compiler will indicate the previous declaration.
了解它的一个简单方法是在编译文件时,编译器会指出先前的声明。

