cout << with char* argument prints string, not pointer value

后端 未结 6 1317
旧时难觅i
旧时难觅i 2020-11-21 07:39

This:

const char * terry = \"hello\";
cout<

prints hello instead of the memory address of the \'h\'.

6条回答
  •  不思量自难忘°
    2020-11-21 07:54

    You should change your code to this:

    cout << static_cast(terry);
    

    The problem is that << operator is overloaded for pointers to C-style strings for printing the content of the string. If you cast it to the raw pointer instead, you will have the default behaviour of printing pointer using iostreams as you want.

提交回复
热议问题