问题
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