質問

I'm a Java developer with very little knowledge of pointers. I have the following C++ code (that someone else wrote) that I'm trying to understand:

ULONGLONG passedValue;
BYTE myArr[16];
memset(myArr, 0x00, sizeof myArr);
*((ULONGLONG *) myArr) = passedValue;

While I understand that the memset method call fills up the myArr byte array (of size 16) with 0s, I'm not sure what the last line does.

According to MSDN, ULONGLONG is 8 bytes...whereas myArr is 16 bytes. What will be in myArr after the last statement is executed?

役に立ちましたか?

解決

This term (ULONGLONG *) myArr will convert myArr to a pointer to ULONGLONG type.

Then, *((ULONGLONG *) myArr) is the value of where the pointer is pointing to.

And,

*((ULONGLONG *) myArr) = passedValue;

assigns passedValue to the first 8 byte of myArr BYTE array.

passedValue 00 00 00 00 00 00 00 00
^^^^^^^^^^^
8 bytes
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top