Results 1 to 7 of 7

Thread: [C++] warning: NULL used in arithmetic

  1. #1
    Join Date
    Aug 2007
    Location
    Novocastria, Australia
    Beans
    751
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    [C++] warning: NULL used in arithmetic

    Okay, say I have a function foo that returns a pointer to an object of type dataType. This function will either return NULL or a pointer to an object.
    Code:
    dataType* foo();
    I want to test if the return value is NULL or not. I use code something like this:
    Code:
    if (foo() == NULL) {
      //do something
    }
    Now, the code in question does what I want it to, I'm just getting this pesky warning:
    Code:
    warning: NULL used in arithmetic
    What's this all about? I want to fix whatever it's warning me about, but I don't see what's wrong with it.


    EDIT: I can post actual code if that will help, this is just mockup of what the situation is.

  2. #2
    Join Date
    Dec 2007
    Location
    UK
    Beans
    571
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: [C++] warning: NULL used in arithmetic

    Looks ok to me. Are you sure its not something else thats causing the problem? Many you could post the code it came from.

    On a side note i prefer using:
    Code:
    if(! foo())
    {
      //do something
    }
    But then I'm lazy

  3. #3
    Join Date
    Aug 2007
    Location
    Novocastria, Australia
    Beans
    751
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: [C++] warning: NULL used in arithmetic

    Wow, silly me. I just realised that my fuction 'foo' actually returns a boolean. I can see why I got the error now. I was comparing a boolean with a zero constant (NULL).

  4. #4
    Join Date
    Aug 2008
    Location
    Israel
    Beans
    151
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [C++] warning: NULL used in arithmetic

    Same thing happened to me

  5. #5
    Join Date
    May 2007
    Beans
    125

    Re: [C++] warning: NULL used in arithmetic

    Isn't NULL defined as ((void*)0)? If that's the case, I don't exactly see why checking 0 == NULL triggers such a warning.

  6. #6
    Join Date
    Jul 2008
    Location
    Dublin, Ireland
    Beans
    633
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: [C++] warning: NULL used in arithmetic

    Quote Originally Posted by NovaAesa View Post
    Code:
    if (foo() == NULL) {
      //do something
    }
    Standard recommendation is using 0 instead of NULL. I know that you've already solved it, but nonetheless...

  7. #7
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: [C++] warning: NULL used in arithmetic

    Quote Originally Posted by Paul Miller View Post
    Isn't NULL defined as ((void*)0)? If that's the case, I don't exactly see why checking 0 == NULL triggers such a warning.
    Because they're of different types. 0 is int, (void *)0 is a pointer.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •