DEV Community

#Shahed_Ali#
#Shahed_Ali#

Posted on

CPP Pointers Doubt !!!

Can some please explain this please
int x=100;
int *ptr;
*ptr=x;
cout<<&x;//Gives address of x;
cout<<&ptr;//Gives address of ptr;
cout<< ptr;//Giving a random address somehow don't know

my doubt is :

whose address is this(ptr),
where & how did my program get hold of this address,
Is this a mistake, bug or a new feature,
Did this break any program,
Is this recommended,
Is this a good practice?;

And How come *ptr var is storing a value of var(x) instead of its address and why is this not an error?

Top comments (1)

Collapse
 
_hs_ profile image
HS

Best guess I can do is that int 100 gets into hex and gets treated as an address thus not causing an error but randomly fetching data without OS kicking in and complaoning. Try outputing value on multiple different program runs in sequence and check If the value stays the same