考虑下面的例子

int nCount[2] = {5,10};
int* ptrInt;
ptrInt = nCount;
cout<<ptrInt<<Endl;//this will print the address of arrar nCount

现在考虑一下
char *str = "Idle mind is a devil's workshop";
int nLen = strlen(str);

char* ptr;
ptr = new char[nLen+1];

strcpy(ptr,str);

cout<<ptr<<endl;//this wil print the string

但这不应该打印str的地址。我不太明白区别。

最佳答案

由于char*经常用于存储字符串,因此ostream operator<<重载,以便char*打印出指向的字符串而不是指针。

如果要输出指针,可以将指针转换为void*,然后输出。

关于c++ - 指针和地址,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2751037/

10-11 22:36
查看更多