Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using ’0’ instead of ’NULL’
QtWS25 Last Chance

Using ’0’ instead of ’NULL’

Scheduled Pinned Locked Moved General and Desktop
12 Posts 9 Posters 31.8k Views
  • 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.
  • N Offline
    N Offline
    Niak74
    wrote on last edited by
    #1

    Hi everyone !

    I'd like to know why Qt intend to uses 0 instead of NULL (null pointer).

    Seen in Qt Documentation, example : QWidget::QWidget ( QWidget * parent = 0, Qt::WindowFlags f = 0 ) (http://doc.qt.nokia.com/4.6/qwidget.html).

    Does Qt headers define NULL as 0 ?

    Some code/dev rules are imposed in my company. So, can I use NULL instead of 0 (ex : QWidget::QWidget ( QWidget * parent = NULL, Qt::WindowFlags f = NULL ) ) or is it risky?

    Regards,
    Niak74

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vsorokin
      wrote on last edited by
      #2

      bq. Bjarne Stroustrup: In C++, the definition of NULL is 0

      "Bjarne Stroustrup's C++ Style and Technique FAQ":http://www2.research.att.com/~bs/bs_faq2.html#null

      --
      Vasiliy

      1 Reply Last reply
      0
      • Y Offline
        Y Offline
        yinyunqiao
        wrote on last edited by
        #3

        0 is always 0, it can never be redefined as anything else.

        1 Reply Last reply
        0
        • O Offline
          O Offline
          ogoffart
          wrote on last edited by
          #4

          This is just a matter of coding style.

          If you whish to use NULL in your code, go for it.

          But Qt code uses 0 because it is more c++ like.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            Niak74
            wrote on last edited by
            #5

            Got it, thanks for your answers !

            Have a nice day !

            Niak74

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kernco
              wrote on last edited by
              #6

              In C, NULL is preferred because depending on the system, it might not be defined as 0 (but I've never seen an example of this). In C++, NULL is defined as 0 in the standard, so both are safely interchangeable. Some people still like using NULL because in their opinion it makes the code clearer. As previous posters have said, it's up to the coder.

              1 Reply Last reply
              0
              • W Offline
                W Offline
                Wolf P.
                wrote on last edited by
                #7

                In most circumstances NULL is more obvious than 0, but it's only a convention.
                (provided that you use 0 only for built-in value types) You can take NULL as an abbreviation for NULL pointer and easier skip the 0 literals, which are mostly trivial.

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Reliance
                  wrote on last edited by
                  #8

                  Defining NULL as anything other than zero is an error. By using NULL, you are opening yourself up to the (unlikely) bug that somebody came along and munged the NULL manifest. But it's a lot harder for them to break 0.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    maciej
                    wrote on last edited by
                    #9

                    "good article":http://mina86.com/2010/10/24/0-is-ambiguous/ about 0/NULL (and nullptr) in C/C++

                    Earth is a beta site.

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      Franzk
                      wrote on last edited by
                      #10

                      There are a lot of companies banning the use of defines and macros just because of the possible risk of breaking a lot of stuff by changing just one define/macro. Since NULL is no more than 0 and not (void*)0 as it sometimes is in C, and it therefore will not generate compiler errors when used inappropriately, I see little value in typing it out.

                      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      1 Reply Last reply
                      0
                      • W Offline
                        W Offline
                        Wolf P.
                        wrote on last edited by
                        #11

                        For your clarification: I'm aware that
                        @ #define NULL 0 @

                        ...and surely I am a define-hater. I used 0 instead of NULL for more than a decade after reading Stroustrup. Today I feel better with NULL written out, maybe tomorrow this will change again. :)

                        1 Reply Last reply
                        0
                        • W Offline
                          W Offline
                          Wolf P.
                          wrote on last edited by
                          #12

                          [quote author="maciek" date="1291899880"]"good article":http://mina86.com/2010/10/24/0-is-ambiguous/ about 0/NULL (and nullptr) in C/C++[/quote] Thanks for the tipp, was interesting to read.

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved