函数声明的顺序很重要,或者我做错了什么? [英] Function declaration order matters in c language or am I doing something wrong?
本文介绍了函数声明的顺序很重要,或者我做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
arthur @ arthur-VirtualBox:〜/ Desktop $ gcc -o hw -ansi hw1。 c
hw1.c:在函数`main'中:
hw1.c:27:16:warning:赋值使整型指针没有转换[默认启用]
hw1.c:在顶层:
hw1.c:69:7:错误:'randomStr'的冲突类型
hw1.c:27:18:注意:以前的`randomStr'隐式声明在这里
编译此代码时:
#include< stdio.h>
#include< stdlib.h>
int main(int argc,char ** argv)
{
char * rndStr;
rndStr = randomStr(FILE_SIZE);
/ *做的东西* /
返回0;
$ b $ *产生一个随机字符串* /
char * randomStr(int length)
{
char * result;
/ *做东西* /
返回结果;
$ b如果我切换函数顺序,它可以工作
为什么? / p>
解决方案 除少数例外情况外,C中的标识符在声明前无法使用。 b
以下是如何在定义之外声明一个函数:
//声明randomStr函数
char * randomStr(int length);
I get this error:
arthur@arthur-VirtualBox:~/Desktop$ gcc -o hw -ansi hw1.c
hw1.c: In function `main':
hw1.c:27:16: warning: assignment makes pointer from integer without a cast [enabled by default]
hw1.c: At top level:
hw1.c:69:7: error: conflicting types for `randomStr'
hw1.c:27:18: note: previous implicit declaration of `randomStr' was here
While compiling this code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
char *rndStr;
rndStr = randomStr(FILE_SIZE);
/* Do stuff */
return 0;
}
/*Generate a random string*/
char* randomStr(int length)
{
char *result;
/*Do stuff*/
return result;
}
If I switch the function order around it works
Why?
解决方案 Except in a few exceptions, an identifier in C cannot be used before it has been declared.
Here is how to declare a function outside its definition:
// Declare randomStr function
char *randomStr(int length);
这篇关于函数声明的顺序很重要,或者我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文