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.

demo: http://coliru.stacked-crooked.com/a/2d134412490ca59a


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -