我今天看到一些奇怪的行为,代码如下:
代码 :
#include <iostream>
struct text
{
char c;
};
int main(void)
{
text experim = {'b'};
char * Cptr = &(experim.c);
std::cout << "The Value \t: " << *Cptr << std::endl ;
std::cout << "The Address \t: " << Cptr << std::endl ; //Print weird stuff
std::cout << "\n\n";
*Cptr = 'z'; //Attempt to change the value
std::cout << "The New Value \t: " << *Cptr <<std::endl ;
std::cout << "The Address \t: " << Cptr << std::endl ; //Weird address again
return 0;
}
问题:
1)我唯一的问题是,为什么cout theAddress
上面的代码会出来一些奇怪的值?
2)为什么我仍然可以改变成员的值c
由dereferenncing其中有古怪地址指针?