I observe some weird behavior today , the code is as follow :
The Code :
#include
struct text
{
char c;
};
int ma
Consider fixing the code like this:
std::cout << "The Address \t: " << (void *)Cptr << std::endl ;
There's a std::ostream& operator<< (std::ostream& out, const char* s );
that takes a char*
so you have to cast to void*
to print an address, not a string it "points" to
I think the "weird" stuff shows up because cout
thinks it's a cstring, i.e. a 0-terminated character array, so it doesn't print the address as you expected. And since your "string" isn't 0-terminated, all it can do is walk the memory until it encounters a 0
. To sum it up, you're not actually printing the address.
Why I can still change the value of the member c by dereferenncing the pointer which has weird address
The address isn't weird, as explained above. In your code Cptr
points to a valid memory location and you can do pretty much anything you want with it.