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

后端 未结 6 1341
旧时难觅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:52

    cout is overloaded so that when you give it a char*, it will print as a pointer to a C-style string. So, it prints out the characters until it hits a null terminating character.

    If you used printf instead of cout, you would see the address. You could also cast the pointer to another type, say (void*) and you would also get the address.

提交回复
热议问题