如何打印第一个字符的地址? [英] How can I print the address of first character?

查看:66
本文介绍了如何打印第一个字符的地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么整个字符串都显示为结果?为什么第一个字符的地址没有打印?如何打印第一个字符的地址?
请帮助我。

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];
}


推荐答案

std :: cout 上的><< 运算符会将 char * 视为空终止的字符串。您需要将其强制转换为 void * c以打印指针值。

The << operator on std::cout will treat a char* as a null-terminated string. You will need to cast it to a void* to print the pointer value.

尝试以下操作:

#include <iostream>

int main()
{
    char x[6] = "hello";
    std::cout << static_cast<void*>(x);
}

这篇关于如何打印第一个字符的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆