문제

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