0

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

sandun dhammika
  • 881
  • 3
  • 12
  • 31

3 Answers3

6

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.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
1

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

Hasturkun
  • 35,395
  • 6
  • 71
  • 104
0
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" )"

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222