2

Im new in programming c with arrays and files. Im just trying to run the following code but i get warnings like that:

23 44 warning: assignment makes pointer from integer without a cast

53 error: expected expression before ‘char’

Any help? It might be silly... but I cant find what's wrong.

#include <stdio.h>

FILE *fp;
FILE *cw;
char filename_game[40],filename_words[40];

int main()
{
    while(1)
    {
         /* Input filenames. */
            printf("\n Enter the name of the file  \n");
            gets(filename_game);
            printf("\n Give the name of the file2 \n");
            gets(filename_words);

         /* Try to open the file with the game */
            fp=fopen(/* args omitted */);                             //line23**
            if   (fp!= NULL)     
            {  
                printf("\n Successful opening %s \n",filename_game); 
                fclose(fp);
                puts("\n Enter x to exit,any other to continue! \n ");
                if ( (getc(stdin))=='x')
                   break;
                else
                    continue;
            }
            else
            {
                fprintf(stderr,"ERROR!%s \n",filename_game);
                puts("\n Enter x to exit,any other to continue! \n");
                if (getc(stdin)=='x')
                   break;
                else
                    continue;
            }

              /* Try to open the file with the names. */            //line 44**
              cw=fopen(/* args omitted */);
             if   ( cw!=NULL )   
            {  
                printf("\n Successful opening %s \n",filename_words); 
                fclose(cw);
                puts("\n Enter x to exit,any other to continue \n ");
                if ( (getc(stdin))=='x')                         
                   break;                                          //line 53**
                else
                continue;
            }
            else
            {
                fprintf(stderr,"ERROR!%s \n",filename_words);
                puts("\n Enter x to exit,any other to continue! \n");
                if (getc(stdin)=='x')
                   break;
                else
                    continue;
            }
    }   
    return 0;
}
FILIaS
  • 495
  • 4
  • 13
  • 26
  • 3
    Did it indicate a line number? What is on that line? – Potatoswatter Apr 25 '10 at 17:43
  • In addition, it's useful to reduce example code before submitting questions, by deleting extraneous bits; this could have been reduced to a five-line example easily. – Brooks Moses Apr 25 '10 at 17:49
  • 1
    Unrelated to your problem, but never, *ever* use `gets`. – jamesdlin Apr 25 '10 at 17:57
  • jamesdlin could u be more specific? why not? im just a beginner and dont know many tips & tricks. – FILIaS Apr 25 '10 at 18:00
  • Are you using an IDE or programmer's editor ? If so can you let us know which line is having the error ? – Romain Hippeau Apr 25 '10 at 22:44
  • i'm using gedit at ubuntu. yes i refer the lines with the errors – FILIaS Apr 25 '10 at 22:54
  • Please start deleting parts of the program to isolate the problem. Start by deleting the bodies of the most deeply nested `if` statements, one at a time. Each time you delete some code, recompile to see if the problem still exists. Update this post with the smallest program you can make that still has the problem. – Dale Hagglund Apr 25 '10 at 23:15
  • @jamesdlin `gets` does not take the length of the result buffer, and if the line to read is larger than the buffer, it overwrite whatever comes after it in the memory. You can replace it by `fgets(filename_game, sizeof(filename_game), stdin)`. Also note that `sizeof` works here because filename_game is an array, it would not work if filename_game would be `char*`. – Rudi Apr 26 '10 at 07:04

2 Answers2

9

You are missing parentheses here:

if (fp=fopen("crypt.txt","r")!=NULL)

The != operator has higher precedence than = so the compiler sees the expression like this:

if ( fp = ( fopen("crypt.txt","r") != NULL ) )

fp gets either 1 or 0 depending on whether fopen returned NULL. fp is a pointer and 0/1 is an integer, hence the warning.

You want

if ( ( fp=fopen("crypt.txt","r") ) != NULL )
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • (For the benefit of the OP) It's generally considered bad programming style to mix assignments or other "side effects" into conditional statements like this, and this question is a prime example of why it's bad. If you move the fp=fopen() onto the previous line (outside the if statement) so you are "only doing one thing at a time" it will be much more readable for any level of programmer. – Jason Williams Apr 25 '10 at 17:58
  • yes u are correct. But it still has the same warning! – FILIaS Apr 25 '10 at 17:58
  • Jason nice tip! Until now i thought that way was the 'good programming style'... thanx for the tip. For the issue, even with the changes, I get the same warnings – FILIaS Apr 25 '10 at 20:26
  • @fil: please update with your new code and your new error messages. Also, is there any change in the program's behavior? Besides the compiler warning, those changes should have fixed it to work properly. – Potatoswatter Apr 25 '10 at 20:39
  • i get the exactly same warnings, potatoswatter! There's none change in the program's behavior. – FILIaS Apr 25 '10 at 21:04
  • ok im not so beginner! yeap i saved it. – FILIaS Apr 25 '10 at 21:17
  • @fil: I'm sorry, but please add some trivial behavior to your program (eg `printf("hello\n");` at the beginning) to verify that changes are taking effect. – Potatoswatter Apr 25 '10 at 21:41
  • but it isn't compiled potatosw! I get the same warnings as before! – FILIaS Apr 25 '10 at 21:47
  • @fil: Warnings shouldn't prevent it from compiling, unless you have the compiler set to strict mode. Anyway, the line numbers should have changed at least, if you followed Jason's style suggestion. – Potatoswatter Apr 25 '10 at 22:12
  • @fil: Now you have the error message pointing at a comment. I'm sorry, but I give up. – Potatoswatter Apr 25 '10 at 22:43
  • that's what the compiler says! ok – FILIaS Apr 25 '10 at 22:46
0

First, it will be much easier for you to debug this problem, and for others to help you, if you try to strip your file down to the minimum example. That aside, if this

fp = fopen( /* args omitted */ );

is giving you a "assignment makes pointer from integer without a cast" warning, then I suspect you don't have a declaration of fopen in scope. Do you by any chance get a warning about an "implict declaration" near the first use of fopen? If so, then you somehow don't have a proper declaration of fopen available, and C automatically decides that it must return an integer.

I don't know why this would be happening, except that the very first line of your example code looks slightly odd:

#include<stdio.h>

Is this really how it appears in your source code? Normally, I'd expect

#include <stdio.h>

I don't recall right now if C should insist on a space between the #include and <stdio.h>.

Dale Hagglund
  • 16,074
  • 4
  • 30
  • 37