-2
#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;
}

When I Try to cout only the pointer it comes in ) 0x(hex value), and I was trying to get only the hex value of it, why is that happening?

João Lima
  • 13
  • 2

1 Answers1

2

it comes in ) 0x(hex value), and I was trying to get only the hex value of it, why is that happening?

Because that is how the IO streams have been specified to behave.

If you want to get rid of the base prefix, you can achieve that by converting the pointer to an integer and using the std::noshowbase manipulator. Integers are shown in decimal base by default though, so if you want the hex value, then use the std::hex manipulator.

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

Accessing y[1] and other indices greater than 0 cause your program to have undefined behaviour.

eerorika
  • 232,697
  • 12
  • 197
  • 326