我有一个小程序来测试将 char* 指针传入和传出函数。 当我用 cc 编译时,我收到警告和错误,说我有冲突类型,即使我所有的变量都是 char* 。请指教
#include <stdio.h>
main()
{
char* p = NULL;
foo1(p);
foo2();
}
void foo1(char* p1)
{
}
char* foo2(void)
{
char* p2 = NULL;
return p2;
}
p.c:11: warning: conflicting types for ‘foo1’
p.c:7: warning: previous implicit declaration of ‘foo1’ was here
p.c:15: error: conflicting types for ‘foo2’
p.c:8: error: previous implicit declaration of ‘foo2’ was here
最佳答案
您需要在 main() 函数之前对您的函数进行原型(prototype)设计。
例子:
void foo1(char *p1);
char* foo2(void);
int main(.......
或者只是将这些函数的主体放在主函数之上。
关于与 char* 冲突的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/885801/