开发者

Why do I get a different value at run-time when type-casting a string to DWORD?

std::cout << (DWORD)"test";

If I compile an开发者_Go百科d run this I get different output values each time, but I can't figure out why.

Any ideas?

PS: I'm using Windows 7 64-bit and I'm compiling with Microsoft Visual C++ 2010 Ultimate.


"test", in your code, is effectively a pointer to the start of the string. When you cast it to a DWORD, your casting the pointer to an integer type, and writing out that number.

As the memory location which is storing "test" can change with each run, the value you see will change.


std::cout << (DWORD)"test";

is equivalent to this:

const char *tmp = "test";
std::cout << (DWORD)tmp; 

That is, it prints the address after casting it into DWORD:

It would print the same value, if you do this also:

std::cout << (const void*)"test";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜