I have the following code:
#include <iostream> char ch[] = "abcd"; int main() { std::cout << (long)(int*)(ch+0) << ' ' << (long)(int*)(ch+1) << ' ' << (long)(int*)(ch+2) << ' ' << (long)(int*)(ch+3) << std::endl; std::cout << *(int*)(ch+0) << ' ' << *(int*)(ch+1) << ' ' << *(int*)(ch+2) << ' ' << *(int*)(ch+3) << std::endl; std::cout << int('abcd') << ' ' << int('bcd') << ' ' << int('cd') << ' ' << int('d') << std::endl; } My question is why the pointer of 'd' is 100 ? I think it should be:
int('d') << 24; //plus some trash on stack after ch And the question is why the second and the third line of the stdout are different ?
6295640 6295641 6295642 6295643
1684234849 6579042 25699 100
1633837924 6447972 25444 100
Thanks.
'abcd'is not a null terminated string.chis only 4 bytes long. Dereferencingch+x(x>0) as anintreads past the allocated memory; the result might change depending on what lies afterchor even be an access violation.