cout 指针时的实际数字


#include<bits/stdc++.h>
using namespace std;
int main(){
int x;
int *y;
x=2;
y= &x;
cout << y << endl;
cout << y[0] << endl; #this cout shows the value of x ( 2 at this case)
cout << y[1] << endl;
cout << y[2] << endl;
cout << y[3] << endl;
return 0;
}

当我尝试只引用指针时,它出现在 ( 0x(十六进制值(,而我试图只获取它的十六进制值,为什么会发生这种情况?

它进来了(0x(十六进制值(,我试图只获取它的十六进制值,为什么会这样?

因为这就是指定 IO 流的行为方式。

如果要删除基前缀,可以通过将指针转换为整数并使用std::noshowbase操纵器来实现。默认情况下,整数以十进制基数显示,因此如果您需要十六进制值,请使用std::hex操纵器。

std::cout << std::noshowbase << std::hex << (std::uintptr_t)y << 'n';

访问大于 0 的y[1]和其他索引会导致程序具有未定义的行为。

最新更新