c++ - Why is the pointer blank when it refers to a value of zero? -
playing pointers in c++. have come across unexpected. have code:
int main(){ char p=0; char* ptr=&p; cout<<"this pointer: "<<ptr; return 0; }
when run code output of ptr
blank. if change value of p
other value seems output random pointer, e.g. value changes between runs, expect. question different assigning char value 0.
info: compiling g++ 4.8.4
char p=0;
your variable defined character , assigned integer value zero, internally assigning null terminated character on variable p. if correct above statement follows , print more details may clear you.
char p='0'; std::cout<<"this pointer: "<<&ptr<<" ---"<<*ptr <<"----"<<ptr;
&&ptr- > getting address of ptr
*ptr-> getting value assigned ptr
ptr-> getting string ptr until see null terminating character, expects garbage value output.
Comments
Post a Comment