Pointer reference and dereference

Posted by ZhekakehZ on Stack Overflow See other posts from Stack Overflow or by ZhekakehZ
Published on 2014-05-27T08:44:24Z Indexed on 2014/05/27 9:25 UTC
Read the original article Hit count: 164

Filed under:

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.

© Stack Overflow or respective owner

Related posts about c++