warning: fopen() call [duplicate]

▼魔方 西西 提交于 2020-01-06 23:18:48

问题


hi I'm programming with stdlib under linux.

The gcc emits the following warning for the following line of code, any idea why is that?

FILE *fd;
if ( fd = fopen( filename, "rw" )== NULL )
{

and the warning is:

warning: assignment makes pointer from integer without a cast.

How this can be happen , according to the stdlib documentation the return type of fopen is FILE*. So why there is a warning still there?Any idea?

--Thanks In Advance--


回答1:


Try

if ((fd = fopen( filename, "rw")) == NULL)
    ^                           ^ 

Otherwise fd will take the value 0 or 1 and the FILE * itself returned by fopen will be lost. So without those parentheses the result of the comparison will be stored in fd instead of the FILE * itself.




回答2:


You are essentially assigning fd to be fopen(filename, "rw") == NULL, as a conditional expression is an integer (0 or 1), you are assigning a pointer from an integer. follow @cnicutar's answer for the fix




回答3:


FILE *fd;
if ( (fd = fopen( filename, "rw" ))== NULL )
{

just copy this code it will work.... you just forget to put () arround "fd = fopen( filename, "rw" )"



来源:https://stackoverflow.com/questions/6980725/warning-fopen-call

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