Printing null pointers

This is a discussion on Printing null pointers within the C++ Programming forums, part of the General Programming Boards category; Whenever I print out the value of pointers, they normally print normally (as in an address in hexadecimal). But if ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    Printing null pointers

    Whenever I print out the value of pointers, they normally print normally (as in an address in hexadecimal). But if I print a null pointer, it only prints 0 and not 0x00etc. The print line is
    Code:
    cout<<this<<":  ("<<this->previous<<", "<<this->next<<", "<<this->element<<")"<<endl;
    I have tried using
    Code:
    cout<<ios::hex<< //values to print
    and also

    Code:
    cout.setf(ios::hex)
    cout<<//line to print
    But each time it still only prints a single 0 when the pointer is null. Is this a Linux issue by any chance? Or a code issue? Thanks.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    A NULL pointer does not point to an address. Traditionally, memory addresses are represented in hexadecimal, which is why you are used to seeing them represented that way.

    But NULL is not an address. A NULL pointer does not point to address 0x00. A NULL pointer points to nothing -- hence, it is a NULL pointer. By the standard, NULL is considered equal to 0 (or false -- a legitimate address, even if invalid, is true). Of course, zero in hexadecimal is 0x00, but probably this representation courtesy of cout is intended to disambiguate address 0x00 from NULL (even tho address 0x00 does not exist anyway).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Ah okay.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Syntax Error??
    By Kennedy in forum C Programming
    Replies: 8
    Last Post: 09-06-2006, 12:04 PM
  2. Replies: 7
    Last Post: 06-16-2006, 10:23 PM
  3. printing list
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 05-16-2006, 12:30 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. Problem with a menu.
    By Rare177 in forum Windows Programming
    Replies: 4
    Last Post: 09-08-2004, 12:51 AM

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