字符数组总能引起我的兴趣。
让我们创建一个整数类型的数组-
int a[]={1,2,34};
cout<<a;
在这种情况下 cout<<a
给我们数组的位置 a
但是-
char a[]="C++";
cout<<a;
给我们C++
作为显示器,还是挺有意思的。
1) 那么为什么字符数组在与 cout 一起使用时不显示其内存位置?
2)如何找到字符数组的位置?
所有帮助将不胜感激:)
- 有学习意愿的 C++ 新手
最佳答案
"1) So why doesnt the array of characters show its memory location when used with cout?"
有一个专门的重载
std::ostream& operator<<(std::ostream&, const char*);
这就是为什么您总是看到打印的字符,而不是地址。
"2) How can I find the location of an array of characters?"
如果您想查看char
数组的地址,您可以转换为void*
指针:
char a[]="C++";
cout << (void*)a;
或者只是将地址运算符作为前缀作为 @WhozCraig建议 their comment
cout << &a;
关于C++ : Arrays and memory locations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27459598/