walterwkh
asked on
copy a byte array
How to copy a byte array to another byte array ? The following is my code :
BYTE* ba2;
void CopyByteArray(LPVOID lpInBuf) // lpInBuf is a byte array
{
ba2 = (BYTE *) malloc(30);
memcpy (ba2, (BYTE*) lpInBuf, sizeof lpInBuf);
}
I have 2 questions here :
1) is it correct to use memcpy ?
2) to pass as an parameter to memcpy, how to get the size of the byte array ? sizeof always gives "4"
Thanks
BYTE* ba2;
void CopyByteArray(LPVOID lpInBuf) // lpInBuf is a byte array
{
ba2 = (BYTE *) malloc(30);
memcpy (ba2, (BYTE*) lpInBuf, sizeof lpInBuf);
}
I have 2 questions here :
1) is it correct to use memcpy ?
2) to pass as an parameter to memcpy, how to get the size of the byte array ? sizeof always gives "4"
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Is that what you want?
To add up to that, 'sizeof(lpInBuf)' wil always equal four on 32bit systems, that's why you have to specify the size additionally.
ASKER
Jaime, if my byte array is not a char array, is it still valid to use memcpy ?
yes, you always can use memcpy, but you have to pay attention to the 'size' argument (the third one). It always should be expressed in bytes.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In fact, you might want to use 'sizeof(DATATYPE)' as a multiplier if yoi are passing the amount of elements. But, 'char' is one byte, so you are fine.