cout fails to print the address of a null char pointer?

Discussion in 'C++' started by zackp, Aug 5, 2008.

  1. zackp

    zackp Guest

    Hello,

    I am learning C++ lately. This morning, out of curiosity I
    constructed a simple code below, but to my surprise, when char* is
    used, the program fails to print almost anything.
    The only thing I got is the following:

    % [0]: np =

    As soon as I use a pointer to another type, everything behaves as
    anticipated. This is a really trivial code, but I am completely
    puzzled. Any hints are appreciated.

    Compiler: gcc version 3.4.2
    Environment: a Dell 1450 laptop running SUN Solaris 10 intel

    Thanks,

    --Zack
    ---------------------------------------- The test code
    ------------------------------------------
    #include <iostream>
    #include <string>

    using std::cout;
    using std::endl;
    using std::string;

    int main(void)
    {
    // if string* is changed to char*, then the program no longer
    prints
    //string* np = 0;
    //string** pnp = &np;
    // should figure out the reason w
    char* np = 0;
    char** pnp = &np;
    cout << "[0]: np = " << np << endl;
    cout << "[1]: pnp = " << pnp << endl;
    cout << "Why the above statements not printing?" << endl;
    return 0;
    }
     
    zackp, Aug 5, 2008
    #1
    1. Advertising

  2. zackp

    Guest

    On Aug 5, 1:22 pm, zackp <> wrote:

    >     // if string* is changed to char*, then the program no longer
    > prints
    >     //string* np = 0;
    >     //string** pnp = &np;
    >     // should figure out the reason w
    >     char* np = 0;
    >     char** pnp = &np;
    >     cout << "[0]: np = " << np << endl;


    operator<< on ostream is overloaded for char* type and will follow the
    pointer to print the characters in that C-style string. You must not
    pass use the null pointer in that case.

    If you want to print the pointer value, then cast it to void* first
    (which is 0 in this case):

    cout << "[0]: np = " << static_cast<void*>(np) << endl;

    >     cout << "[1]: pnp = " << pnp << endl;


    No problem with that one, because it is the address of np and is not
    NULL.

    Ali
     
    , Aug 5, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    6,800
    Ron Natalie
    Jun 10, 2005
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,061
    Ian Collins
    May 9, 2006
  3. Ralf Goertz

    Null pointer to cout

    Ralf Goertz, May 24, 2006, in forum: C++
    Replies:
    14
    Views:
    665
    C. Benson Manica
    May 25, 2006
  4. aneuryzma
    Replies:
    3
    Views:
    717
    Jim Langston
    Jun 16, 2008
  5. Christopher
    Replies:
    4
    Views:
    445
    Ruben Safir
    Jul 9, 2011
Loading...

Share This Page