Bloomberg LP Interview Question
Software Engineer / Developersa bit of correction in the question..
actually the question is:
cout<<*(int *)(p + i)<<"\n"; instead of cout<<*(int *)p<<"\n";
if it is (p+i) like the op said...it would give a seg-fault/bus error..otherwise 255 4 times..
Depending on endianness, when we do typecast to char *, i would fetch either Hex x00 or xFF. When we again convert from char* to Int*, the 00 or FF would go to lower byte but upper byte might have anything which is in memory so logically it should be random output.
The data width of the machine will be logically equal to larger than ints. So it is a 32 bit machine, ints should be in 32 bits which is 4 bytes. I think the addressing scheme will be limited only to 32 bits or 4 bytes. so p will point to 4 bytes, p+1 will point to a next memory location which is again 4 bytes (but some random value)
1. The number of iterations depends on sizeof(int) which is platform dependent.
- sergei June 02, 20102. The first value will be 255 since the pointer doesn't change if i==0.
3. Then the behavior is undefined.