简体   繁体   English

如何打印第一个字符的地址?

[英]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.

相关问题 如何在C中没有空格的情况下打印NUL字符? - How can I print a NUL character without a space in C? 如何在C ++中打印“█”特殊字符? - How can I print this“█” special character in C++? 如何获得我的代码以随机打印字符或字符串? - How can i get my code to print a character or string randomly? 如何删除字符串中的第一个和最后一个字符? - How can I erase first and last character in string? 如何将一个字符数组中的前i个字符复制到C ++中的另一个字符数组中? - How can I copy the first i amount of characters in a character array into another character array in C++? 我如何处理 .txt 文件中的字符,就像在 C++ 中处理数组一样? - how can i address a character in a .txt file as i would do with an array in c++? 如何在主函数中打印字符数组的内容,该函数的基地址由函数通过字符指针返回? - How to print the contents of a character array in the main function whose base address is returned by a function through a character pointer? 如何使用(返回地址)函数打印“*cp”??(请参考我的代码) - How can I print " *cp " using (Return address )function??( please refer my code) 有没有办法可以将字符串的地址存储到二维字符数组中 - Is there a way I can store address of a string to a 2D character array 打印字符数组地址的正确方法 - Proper way to print address of character array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM