指针和地址

ckv*_*ckv 1 c++ pointers

考虑下面的例子

int nCount[2] = {5,10};
int* ptrInt;
ptrInt = nCount;
cout<<ptrInt<<Endl;//this will print the address of arrar nCount
Run Code Online (Sandbox Code Playgroud)

现在考虑一下

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 
Run Code Online (Sandbox Code Playgroud)

但不应该打印str的地址.我不是很有所作为.

Jam*_*lis 7

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

如果要输出指针,可以将指针强制转换void*然后输出.