change pointer of char[] in c++ -


i had write program declares char text[16]; , int number; , fills input user. have call function (without passing anything) , recall data stack , display it. able find both of them

// function called without passing variables int   number;  char  text[16];  cout << *(&text + 5) << endl; // outputs correct text can not figure out how assign text number = *(int*)(&number + 20); // works , outputs correct number , saves correctly int number;   cout << "\tnumber: "   << number   << endl; cout << "\ttext:   "   << text   << endl; 

i know if there way transfer text *(&text + 5) char text[16];

what doing going result in undefined behavior. depending heavily on compiler , therefore in turn on architecture. proof being when compile , run code different optimization flags different results. check out https://en.wikipedia.org/wiki/buffer_overflow

the first expression *(&text + 5) evaluates taking address of text pointer in memory, adding 5 , dereferencing character value stored @ location. can pretty on stack @ time.

&number + 20 go off "visible" stack, pointer arithmetic , result in adding 20*sizeof(int) pointer memory address of number on stack.

if intending should use strcpy suggested @jafferwilson


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -