Results 1 to 7 of 7

Thread: Pointers: NULL vs. 0

  1. #1
    Join Date
    Jan 2010
    Location
    Perth, Australia
    Posts
    37
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Pointers: NULL vs. 0

    Hi, I noticed that Trolltech's code examples use 0 instead of NULL to represent null pointers. Does it matter which one I use?

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Pointers: NULL vs. 0

    No it doesn't matter. A pointer is in the end just an integer. The null pointer can either be:
    Qt Code:
    1. #define NULL 0
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. #define NULL ((void *)0)
    To copy to clipboard, switch view to plain text mode 

    More often than not, the former is used.

    In the C++0x standard that is still in the making, plans are to deprecate the above definitions and introduce a null pointer called nullptr_t, to prevent the following confusion:
    Qt Code:
    1. void func(int i);
    2. void func(const char *t);
    To copy to clipboard, switch view to plain text mode 
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. The following user says thank you to franz for this useful post:

    hackerNovitiate (31st January 2010)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Pointers: NULL vs. 0

    You shouldn't (or at least needn't) use NULL in C++ code. NULL is a C macro while C++ has "null" and "0" defined, you should use either of them.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #4
    Join Date
    Jan 2010
    Location
    Perth, Australia
    Posts
    37
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Pointers: NULL vs. 0

    Quote Originally Posted by wysota View Post
    You shouldn't (or at least needn't) use NULL in C++ code. NULL is a C macro while C++ has "null" and "0" defined, you should use either of them.
    I tried to compile
    Qt Code:
    1. int *p = null;
    To copy to clipboard, switch view to plain text mode 
    but g++ reported "error: 'null' was not in this scope". Do I need to #include something?

    Also, I was directed to Bjarne Stroustrup's (creator of C++) website at http://www2.research.att.com/~bs/bs_faq2.html#null where he said "In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0" which sounds like NULL is ok in C++...?

    Sorry for posting in the wrong forum; I thought it was Qt thing instead of a C++ thing.

  6. The following user says thank you to hackerNovitiate for this useful post:

    genomega (1st February 2010)

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Pointers: NULL vs. 0

    Hmm... maybe there is no "null"... I though there was but maybe it was just a macro to 0.

    NULL is ok as it is most often defined as "0". But NULL in C is theoretically "0L" which makes a difference in length. But there is no practical difference, apart from aesthetics - if you get paid by the character, it's better to write "NULL", it's three chars longer
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #6
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Pointers: NULL vs. 0

    Hi, I noticed that Trolltech's code examples use 0 instead of NULL to represent null pointers. Does it matter which one I use?
    Use 0 .

  9. #7
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Pointers: NULL vs. 0

    Stroustrup prefers that you use 0 rather than NULL, because in C++ it is defined as 0 and he prefers to NOT use macros.

    http://www2.research.att.com/~bs/bs_faq2.html#null

    The next version of C++ will use a keyword nullptr for this. If you choose to use NULL now, it will be easier to find and replace NULL with nullptr later.

Similar Threads

  1. setRelation and NULL in ID
    By Auryn in forum Qt Programming
    Replies: 4
    Last Post: 26th May 2015, 16:20
  2. Null for QDateEdit
    By wirasto in forum Qt Programming
    Replies: 1
    Last Post: 16th November 2009, 21:54
  3. QPixmap seems to be null but it isn't
    By satoshi in forum Qt Programming
    Replies: 7
    Last Post: 8th May 2009, 09:23
  4. QSqlTableModel and NULL
    By karas in forum Qt Programming
    Replies: 0
    Last Post: 9th February 2009, 12:55
  5. checking for null
    By drkbkr in forum General Programming
    Replies: 6
    Last Post: 13th March 2006, 21:54

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.