c++ - No idea what this pointer could be pointing to and unable to interpret the result -
this given past question in exam i'm unable understand result obtained of last 4 printf functions. conversion hexadecimal first 2 don't see how there characters @
ptr[0] ptr[3]   this section of code compiled , run.
int main(int argc, char *argv[]){      typedef unsigned char byte;      unsigned int nines = 999;     byte * ptr = (byte *) &nines;      printf ("%x\n",nines);     printf ("%x\n",nines * 0x10);     printf ("%d\n",ptr[0]);     printf ("%d\n",ptr[1]);     printf ("%d\n",ptr[2]);     printf ("%d\n",ptr[3]);      return exit_success; }   and corresponding output
3e7 3e70 231 3 0 0      
when byte * ptr = (byte *) &nines; set address of ptr same address of nines. has value of 999 , in hex 0x3e7
from problem, assuming int has 4 bytes , little endian system. i.e. bytes stored this.
--------------------------------- | 0xe7  |  0x03 |  0x00 |  0x00 | ---------------------------------   ptr     ptr+1   ptr+2    ptr+3   so when print them out, values of 231, 3, 0 , 0  (231 equal 0xe7)
in little endian system, followed intel processors , microcontrollers today, least significant byte stored first , significant byte stored last.
on other hand, have big endian system, followed older motorola controllers , power pc's. in significant byte stored first. output in systems 0, 0, 3 , 231.
Comments
Post a Comment