what is the deffrence between (!fp) and (fp == NULL) //FILE *fp = NULL

This is a discussion on what is the deffrence between (!fp) and (fp == NULL) //FILE *fp = NULL within the C Programming forums, part of the General Programming Boards category; Hello,... i got a strange beahvioer with NULL and 0 (when working on SMSer progect source attached): This code will ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    112

    what is the deffrence between (!fp) and (fp == NULL) //FILE *fp = NULL

    Hello,...

    i got a strange beahvioer with NULL and 0 (when working on SMSer progect source attached):

    This code will work fine for me :
    Code:
    int CalcPendingSMS()
    {
        FILE *fp = NULL;
        char tmp[MAXLINELENGTH];
        int counter=0; //array counter
    	
         //add home path to centericq
         char OfflinePath[MAXLINELENGTH];
         sprintf(OfflinePath,"%s%s", HOMEDIR,OFFLINEFILE);
    	
         fp=fopen(OfflinePath,"r");
         printf("%d",fp);
         if (!fp)
         {
        	return -1;
         }
         while (fgets(tmp,MAXLINELENGTH,fp)!=NULL)
            counter++;
            
         fclose(fp);
        
         counter=(counter) /7;    
         return counter;
    }
    but this won't (SIGSEGV error )
    Code:
    int CalcPendingSMS()
    {
        FILE *fp = NULL;
        char tmp[MAXLINELENGTH];
        int counter=0; //array counter
    	
        //add home path to centericq
        char OfflinePath[MAXLINELENGTH];
        sprintf(OfflinePath,"%s%s", HOMEDIR,OFFLINEFILE);
    	
        fp=fopen(OfflinePath,"r");
        printf("%d",fp);
        if (fp == NULL)
        {
        	return -1;
        }
        while (fgets(tmp,MAXLINELENGTH,fp)!=NULL)
            counter++;
            
        fclose(fp);
        
        counter=(counter) /7;    
        return counter;
    }
    but the problem is that afaik there isn't any real difference between that two.
    Attached Files Attached Files
    Last edited by jabka; 10-19-2007 at 06:09 PM.
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    fp == NULL compares fp to NULL
    !fp compares against 0

    NULL need not be defined as zero.

    From your description of the problem it would seem that NULL is indeed not equal to 0. You could printf() the value of NULL to your terminal to check this.

    P.S. You should do something about your indentation.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,689
    But if !fp can be true, as jabka states, then there is no way NULL isn't zero. fopen returns NULL on failure, after all.

    SIGSIEV errors can occur anywhere within the program after you've corrupted the memory pool or accessed things that don't belong to you. To be really sure that your function isn't causing the problem, then you need to do unit testing.
    Code:
    int CalcPendingSMS( void );
    
    #include <stdio.h>
    #define MAXLINELENGTH   512uL
    #define HOMEDIR         "C:/Documents and Settings/JCK/Desktop/"
    #define OFFLINEFILE     "dump.txt"
    int main( void )
    {
        printf( "CalcPendingSMS returned %d successfully.\n",
            CalcPendingSMS()
        );
        return 0;
    }
    
    int CalcPendingSMS( void )
    {
        FILE *fp = NULL;
        char tmp[MAXLINELENGTH];
        int counter=0;
       
         /* add home path to centericq */
         char OfflinePath[MAXLINELENGTH];
         sprintf(OfflinePath,"%s%s", HOMEDIR, OFFLINEFILE);
    	
        fp=fopen(OfflinePath,"r");
        printf("%p\n", (void*)fp);
        if (fp == NULL)
        {
        	return -1;
        }
        while (fgets(tmp, MAXLINELENGTH, fp) != NULL)
        counter++;
        fclose(fp);
         
        counter = counter / 7;
        return counter;
    }
    I didn't observe any SIGSEIV error. Perhaps your LINELENGTHs are too small or something else.
    Last edited by whiteflags; 10-19-2007 at 06:32 PM.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    112
    since MAXLINELENGTH is used only to store the line (in fgets) i can't understand why the only change to get the error is to change the fp == NULL to !fp.


    just for clearfiing :
    $smser -t
    on debian etch kernel 2.6.22
    was used to get the error.
    Last edited by jabka; 10-19-2007 at 06:57 PM.
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    112
    Ill try (tomorrow it is 2:37 am here) to search for potential memory corruption.
    maybe it was just a strange quintessence.
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,689
    That's a stupid function call. feof( fp ) and !fp do not do anything close to the same thing.

    feof( fp ) tests the eof bit of the file stream pointed to by fp, but regardless fp is still a valid pointer. The man page for feof() doesn't say anything about correctly handling a NULL argument, which is where you have your problem. Compare fp with NULL first, before you call that.

    A recently opened file will result in feof( fp ) returning 0 also.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    112
    sorry i mistiped it should be fp == NULL and !fp (it abit late )
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    There are places in your code where you do this.
    Code:
        if (fp = NULL)
    Rather that simply be a comparison, it is an assignment of NULL to fp and automatic failure of the if condition. I'd double check your code and fix these issues.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    983
    To be clear: There is no difference between
    Code:
    (!fp)
    and
    Code:
    (fp == NULL)
    It makes no difference how NULL is defined; those will both yield the same thing (assuming, of course, fp is indeed a pointer).

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    Even with basic levels of compiler warnings, we get
    Code:
    $ gcc -Wall -c foo.c
    foo.c: In function `fGetnumberfile':
    foo.c:71: warning: suggest parentheses around assignment used as truth value
    foo.c: In function `fSetLineStr':
    foo.c:124: warning: suggest parentheses around assignment used as truth value
    foo.c:131: warning: suggest parentheses around assignment used as truth value
    foo.c: In function `sendSMS':
    foo.c:321: warning: suggest parentheses around assignment used as truth value
    foo.c: In function `CalcPendingSMS':
    foo.c:432: warning: int format, pointer arg (arg 2)
    
    $ gcc -W -Wall -c foo.c
    foo.c: In function `fGetnumberfile':
    foo.c:71: warning: suggest parentheses around assignment used as truth value
    foo.c: In function `fSetLineStr':
    foo.c:124: warning: suggest parentheses around assignment used as truth value
    foo.c:131: warning: suggest parentheses around assignment used as truth value
    foo.c: At top level:
    foo.c:227: warning: unused parameter 'a'
    foo.c: In function `ValidMessage':
    foo.c:284: warning: comparison between signed and unsigned
    foo.c: In function `sendSMS':
    foo.c:321: warning: suggest parentheses around assignment used as truth value
    foo.c: In function `CalcPendingSMS':
    foo.c:432: warning: int format, pointer arg (arg 2)
    
    $ gcc -W -Wall -O2 -c foo.c
    foo.c: In function `fGetnumberfile':
    foo.c:71: warning: suggest parentheses around assignment used as truth value
    foo.c: In function `fSetLineStr':
    foo.c:124: warning: suggest parentheses around assignment used as truth value
    foo.c:131: warning: suggest parentheses around assignment used as truth value
    foo.c: In function `ValidMessage':
    foo.c:284: warning: comparison between signed and unsigned
    foo.c: In function `sendSMS':
    foo.c:321: warning: suggest parentheses around assignment used as truth value
    foo.c: In function `CalcPendingSMS':
    foo.c:432: warning: int format, pointer arg (arg 2)
    foo.c: At top level:
    foo.c:227: warning: unused parameter 'a'
    foo.c: In function `AccountParserLine':
    foo.c:453: warning: 'c' might be used uninitialized in this function
    
    If you run code with these kinds of warnings still present in the code, then expect to get a spanking.

    The colours just highlight the additional warnings which appear when you ramp up the warning levels of the compiler.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 11:25 AM
  4. Replies: 4
    Last Post: 11-23-2003, 06:15 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21