I need to pass an unsigned char array from one method to another, and i tried using this code:
{
unsigned char *lpBuffer = new unsigned char[182];
ReceiveSystemState(lpBuffer);
}
BOOL ReceiveSystemState(unsigned char *lpBuffer)
{
unsigned char strRecvBuffer[182] = { 0 };
//strRecvBuffer construction
memcpy(lpBuffer, strRecvBuffer, sizeof(strRecvBuffer));
return TRUE;
}
Neither of those 3 methods (used in ReceiveSystemState
) worked as i expected. After using each one of them all that it is copied is the first char from strRecvBuffer
and nothing more. The strRecvBuffer
has empty chars from element to element, but i need those as they are, because that string is a message from a hardware device and that message will be anallysed using a protocol. What do i miss here? Do i initialize lpBuffer
wrong?
EDIT: I've used a simple memcpy
method to do the job. Still the same result: all that it is copied is the first char of strRecvBuffer
.
EDIT2: Working code:
{
unsigned char *lpBuffer = new unsigned char[182];
ReceiveSystemState(lpBuffer);
for (int i = 0; i < 144; i++)
{
memcpy(&c_dateKG[i], lpBuffer + i * sizeof(unsigned char), sizeof(unsigned char) );
}
}
BOOL ReceiveSystemState(unsigned char *lpBuffer)
{
unsigned char strRecvBuffer[182] = { 0 };
//strRecvBuffer construction
memcpy(lpBuffer, strRecvBuffer, sizeof(strRecvBuffer));
return TRUE;
}