Skip to content
Advertisement

Unsigned long into char array

Here is an example of my code :

JavaScript

My question is, why does this last memcpy works ? i would expect to put CRYPTO and not &CRYPTO in arguments… For me, CRYPTO is the value i want so 0xe8ba8fa3 and &CRYPTO the address. And for me, CRYPTO is not a pointer, so why i need to use memcpy with &CRYPTO to make it works ?

By the way, my code may be pure disaster, i’m a beginner. Do not hesitate to correct me !

Thank you !

Advertisement

Answer

My question is, why does this last memcpy works ? i would expect to put CRYPTO and not &CRYPTO in arguments… For me, CRYPTO is the value i want so 0xe8ba8fa3 and &CRYPTO the address.

You’re right. CRYPTO is not a pointer. However, memcpy expects a pointer, so we have to give it one. We do this by taking CRYPTO ‘s address, and this is done by adding & to it, hence &CRYPTO.

What memcpy does is copying the memory from one address to the other address (that’s why it takes two pointers), regardless of the actual content at those addresses. If you gave it CRYPTO instead of a pointer to it, it would likely interpret the value of CRYPTO as an address (the behavior is undefined, there’s no guarantee for what will happen unless the compiler gives one).

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement