字符类型变量的地址
The address of character type variable
这里只是一个测试原型:
#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"的地址是字符类型,而不是打印?
<<
有一个重载,它获取指向char
的指针并将其解释为终止的 C 样式字符串。将其用于任何其他char
的地址将出现可怕的错误。
无类型指针,这样<<
就不会变得太聪明:
cout << static_cast<void*>(&b)
表达式&b的类型为char *。当运算符<<对 char * 类型的对象使用时,它会将其视为字符串并将其输出为字符串。要输出您应该写的地址
( void * ) &b
或
reinterpret_cast<void *>( &b )
代码中的<<
运算符在第 11 C++重载。它与任何其他类型的int
或string
不冲突,但它需要指向char
的指针,如果使用,可能会产生不希望的结果。
你可以这样做:-
cout << static_cast<void*>(&b)
相关文章:
- 通过按地址访问变量
- 从模板创建通用打印函数,以打印基元数据类型变量的值
- 初学者问题:C++指针/地址 - 和变量之后不是以前?
- 指针数据类型变量如何包含对象?
- 将 static_cast<int>(-15) 分配给静态常量字符类型变量
- 将内置类型变量传递给只有一个类类型参数的"+"运算符函数时自动类型转换的构造函数
- 如何为结构类型变量赋值
- 如何将字符类型变量附加到字符串中?
- 共享或私有 openmp 代码中的结构化类型变量
- 有没有办法在 c++ 中制作类型变量?
- 从其他类声明类类型变量会给出 C2143 错误(在"*"之前缺少";")
- 如何检查类中定义的枚举类型变量是否被分配给?
- 将子类实例保存在父类型变量中并通过父变量使用 Child 函数?
- 如何使用构造函数初始化内存地址(指针变量)?
- 是否可以在没有结构的情况下创建新的类型变量?
- 为什么类型变量;不调用默认 CTR
- 如何使用CIN将输入整数值获取到char类型变量中
- 如何为 char** 类型变量提供适当的值,以便将其分配给字符串变量
- 字符类型变量的地址
- 在C/ c++或任何其他类似语言中,对变量或数据类型应用&符号时返回的地址类型