[英]How can I print the address of first character?
Why the entire string is displayed as outcome? 为什么整个字符串显示为结果? Why address of 1st character not getting printed? 为什么第一个字符的地址没有被打印? How can I print address of 1st character? 如何打印第一个字符的地址? Please help me. 请帮我。
#include <iostream>
int main()
{
char x[6]="hello";
std::cout<<&x[0];
}
The <<
operator on std::cout
will treat a char*
as a null-terminated string. std::cout
上的<<
运算符将char*
视为以null结尾的字符串。 You will need to cast it to a void*
to print the pointer value. 您需要将其强制转换为void*
以打印指针值。
Try this: 试试这个:
#include <iostream>
int main()
{
char x[6] = "hello";
std::cout << static_cast<void*>(x);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.