Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. (NULL == p) vs (p == NULL)

(NULL == p) vs (p == NULL)

Scheduled Pinned Locked Moved C / C++ / MFC
visual-studiohelp
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fitatc
    wrote on last edited by
    #1

    what different between (NULL == p) and (p == NULL) I realy confused about this, Any advise will be help.

    H P C A 4 Replies Last reply
    0
    • F fitatc

      what different between (NULL == p) and (p == NULL) I realy confused about this, Any advise will be help.

      H Offline
      H Offline
      hanq_38910130
      wrote on last edited by
      #2

      If you accidentally write "=", but not "==" which you originally want it to be, then in the first case, compiler will tell you this mistake.But in the second case, compiler will not do this. This only prevent you to make mistakes, but I don't think this is necessary:)

      J 1 Reply Last reply
      0
      • H hanq_38910130

        If you accidentally write "=", but not "==" which you originally want it to be, then in the first case, compiler will tell you this mistake.But in the second case, compiler will not do this. This only prevent you to make mistakes, but I don't think this is necessary:)

        J Offline
        J Offline
        Joe Woodbury
        wrote on last edited by
        #3

        Most newer compilers will give you a warning for doing an assignment operator in a conditional statement.

        H 1 Reply Last reply
        0
        • J Joe Woodbury

          Most newer compilers will give you a warning for doing an assignment operator in a conditional statement.

          H Offline
          H Offline
          hanq_38910130
          wrote on last edited by
          #4

          I don't know which compliers you refer to, but when I test it with VS2005(which I am using now), it doesn't give any warning.Of course, here I'm using the default warning level: level 3. When I change it to Level 4, it indeed give a warning:)

          P 1 Reply Last reply
          0
          • H hanq_38910130

            I don't know which compliers you refer to, but when I test it with VS2005(which I am using now), it doesn't give any warning.Of course, here I'm using the default warning level: level 3. When I change it to Level 4, it indeed give a warning:)

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            You should always use level 4. There shouldn't be lesser levels.

            1 Reply Last reply
            0
            • F fitatc

              what different between (NULL == p) and (p == NULL) I realy confused about this, Any advise will be help.

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              That's a pattern that is generally held to be rubbish nowadays. It only works when comparing an lvalue with an rvalue, and if you can remember to do it then you can remember to use the intended operator.

              1 Reply Last reply
              0
              • F fitatc

                what different between (NULL == p) and (p == NULL) I realy confused about this, Any advise will be help.

                C Offline
                C Offline
                Chuck OToole
                wrote on last edited by
                #7

                I've always viewed the difference as purely cosmetic or "style" of the coder. Personally, I've always disliked the (NULL == p) style because when I look at code for debugging, my brain processes left to right so I want to read "if p is NULL".

                1 Reply Last reply
                0
                • F fitatc

                  what different between (NULL == p) and (p == NULL) I realy confused about this, Any advise will be help.

                  A Offline
                  A Offline
                  Adam Roderick J
                  wrote on last edited by
                  #8

                  There is no difference between (NULL == p) or (p == NULL) Both are same. Actually it always better to prefer ( NULL == p ) Why because, To aviod the accident of assignment that can happen. say you are writing a statement (p == NULL ), but accidentally you wrote as p = NULL then the whole meaning is changed. But if you use ( NULL == p ) then forgetting to give NULL = p will give compiler error. From early days of programming this was a common error, so it now a standard to use as (NULL == p), which applicable to all the constants. It is always better to write constants on right side.

                  Величие не Бога может быть недооценена.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups