Function declaration order matters in c language or am I doing something wrong?

旧时模样 提交于 2019-12-04 04:35:26

问题


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?


回答1:


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);



回答2:


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:


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) {...}).

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.

As of C99, implicit typing is no longer allowed.

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

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.




回答4:


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().

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).

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.




回答5:


Just add a function prototype

#include <stdio.h>
#include <stdlib.h>

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;
}


来源:https://stackoverflow.com/questions/13297233/function-declaration-order-matters-in-c-language-or-am-i-doing-something-wrong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!