C++用字符串存储位置的指针来表示字符串,指针的形式可以是数组、wchar_t(char)指针。
但是对于其他类型的指针,C++将其对应于void*,并将其地址打印出来。所以,如果想要打印字符串的地址,将其指针强制转换成其他类型的指针即可,如下:
using std::endl;
using std::wcout;
setlocale(LC_ALL, "");
wchar_t strArr[6] = L"锄禾日当午";
wchar_t* pStr = L"汗滴禾下土";
wcout << strArr << endl; //打印的都将是字符串
wcout << pStr << endl;
wcout << (void*)pStr << endl; //想打印地址,必须强制转换成非字符串指针
int* pValue = new int(10);
wcout << pValue << endl; //打印的是地址
delete pValue;
pValue = nullptr;
打印结果: