字符类型变量的地址 [英] The address of character type variable

查看:108
本文介绍了字符类型变量的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只是一个测试原型:

#include <iostream>
#include <string>
using namespace std;

int main()
{
   int a=10;
   char b='H';
   string c="Hamza";

   cout<<"The value of a is : "<<a<<endl;
   cout<<"The value of b is : "<<b<<endl;
   cout<<"The value of c is : "<<c<<endl<<endl;

   cout<<"address of a : "<<&a<<endl;
   cout<<"address of b : "<<&b<<endl;
   cout<<"address of c : "<<&c<<endl;

   return 0;
}

为什么字符类型的变量'b'的地址不打印?

Why the address of variable 'b', which is of character type, not printing?

推荐答案

您的代码中的<<运算符在C ++ 11中已重载.它与intstring之类的任何其他类型都没有冲突,但是它需要指向char的指针,如果使用该指针会产生不希望的结果.

The << operator in your code is overloaded in C++ 11. It doesn't conflict with any of other types like int or string, but it takes pointer to char which if used can produce undesired results.

您可以这样做:-

cout << static_cast<void*>(&b)

这篇关于字符类型变量的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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