wrong error在C语言中,函数声明顺序在c语言中很重要,还是我做错了什么?

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

#include

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?

如果我改变函数的顺序为什么?

5 个解决方案

#1

6

Except in a few exceptions, an identifier in C cannot be used before it has been declared.

除了少数例外,在声明之前不能使用C中的标识符。

Here is how to declare a function outside its definition:

下面是如何在其定义之外声明函数:

// Declare randomStr function

char *randomStr(int length);

#2

5

The error occurs because symbols must generally be declared before they are used, not after.

之所以会出现错误,是因为符号通常必须在使用之前声明,而不是在使用之后。

A special exception to that is functions. If they are used before declaration, a particular declaration is inferred. In your case, the inferred declaration did not match the eventual definition.

一个特殊的例外是函数。如果在声明之前使用它们,则推断特定的声明。在您的例子中,推断的声明与最终的定义不匹配。

#3

2

You need to put a function declaration before any functions that use a function that isn't defined yet.

您需要在使用尚未定义的函数的任何函数之前放置一个函数声明。

For example:

例如:

int doesFileExist(const char* str);

would go before main().

会在main()。

The reason for this is that the C compiler runs through your code, and every time it sees a function, it looks to see if the function has been defined yet. If it can't find that function, it gives you an error. main() uses doesFileExist(), which has not been declared by the time the compiler looks at main(). To fix this, you can make a declaration of a function at the top, as I mentioned above. This tells the compiler that, though a function has not been filled in yet, it will be filled in later and so to treat it as if the function does exist, then go connect them later once you've found the function later on (ie, somewhere after the main() function).

原因是C编译器在您的代码中运行,每次它看到一个函数时,它就会查看函数是否已经定义。如果它找不到那个函数,它会给你一个错误。main()使用doesFileExist(),编译器查看main()时还没有声明它。要解决这个问题,您可以在顶部声明一个函数,正如我上面提到的。这告诉编译器,虽然一个函数还没有被填充,但它将在稍后填充,因此要将它视为该函数确实存在,然后在稍后(即main()函数之后的某个地方)找到该函数后再连接它们。

Your second block of code works because the compiler has definitions for those functions which you use in main() by the time it gets there. Your first block does not work because the functions neither defined nor declared that they will be defined later.

第二个代码块可以工作,因为编译器在main()中使用这些函数时已经有了定义。第一个块不起作用,因为函数既没有定义也没有声明以后将定义它们。

#4

2

In C89 and earlier, if the compiler saw a function call before a corresponding declaration or definition, it assumed that the function returned int. Similarly, if you left the type specifier off of the function definition, it was assumed to return int (this is why a lot of older examples have an entry of just main() {...} instead of int main(void) {...}).

在C89和早些时候,如果编译器看到一个函数调用相应的声明或定义之前,它假定函数返回int。同样,如果离开了类型说明符的函数定义,它是假定返回int(这就是为什么很多老的例子有一个条目的主要(){…}代替int main(void){…})。

Since the call to randomStr occurs before its declaration/definition, it is assumed to return int, which is not compatible with char * (you can't assign a value of one to the other without an explicit cast), hence the error.

由于对randomStr的调用发生在其声明/定义之前,所以假定它返回int,这与char *不兼容(如果没有显式转换,您不能将一个值赋给另一个值),因此会出现错误。

As of C99, implicit typing is no longer allowed.

从C99开始,隐式类型不再被允许。

The -ansi option causes gcc to compile as C89. To compile as C99, use -std=c99 instead.

-ansi选项导致gcc编译为C89。要编译为C99,可以使用-std= C99。

I generally put the function definition before its first use within the same file, so my code reads from the bottom up (main is typically the last function defined in the file). That way I don't have to worry about keeping declarations and definitions straight.

我通常将函数定义放在同一个文件中第一次使用之前,因此我的代码从底向上读取(main通常是文件中定义的最后一个函数)。这样,我就不必担心声明和定义是否正确。

#5

1

Just add a function prototype

只需添加一个函数原型。

#include

#include

char* randomStr(int length);

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值