Вот пример моего кода:
/* Standard Linux headers */
/* --------------------------------------------------------------------------
Calculates the CRYPTO
-------------------------------------------------------------------------- */
unsigned long CalculateCRYPTO(
unsigned long ulCount, /* Number of bytes in the data block */
unsigned char *ucBuffer ) /*Data block*/
{
unsigned long ulCRYPTO = 0;
//fonction that i have not coded ...
return( ulCRYPTO );
}
int main (void)
{
/*Variables and socket programming*/
//this is my datablock must be in hexa AA 35 07 (will chnage size and data but for now it's hardcoded)
unsigned char datablock[3];
memset(datablock, '\0' ,sizeof(datablock));
datablock[0]=0xaa;
datablock[1]=0x35;
datablock[2]=0x07;
unsigned long CRYPTO;
CRYPTO=CalculateCRYPTO(sizeof(datablock),datablock); //calculate crypto depending of datablocks
printf("CRYPTO = 0x%08x \n", CRYPTO); //prints me 0xe8ba8fa3 that is what i want
/*Creating the final command*/
//(will chnage size and data but for now it's fixed)
unsigned char cmd_final_hex[7]; //this needs to be DATABLOCKS+CRYPTO
//in hexa AA 35 07 concatenated to inverted CRYPTO so ... A3 8F BA E8 => must be AA 35 07 A3 8F BA E8
memset(cmd_final_hex, '\0' ,sizeof(cmd_final_hex)); //Make sure cmd final is at 0
memcpy(cmd_final_hex, datablock, sizeof(datablock)); //cmd at datablock + 000
// while loop prints me what i want so cmd_final_hex[]=AA 35 07 00 00 ...
//Now i want to concatenate so my idea is to use memcpy and not strcat :
memcpy(&cmd_final_hex[sizeof(datablock)], &CRYPTO, 4);
//and a print gives me AA 35 07 A3 8F BA E8 which is exactly what i want but why do i have to use "&CRYPTO" and not "CRYPTO" in my memcpy. !!!
return 0;
}
Мой вопрос: почему работает этот последний memcpy?я ожидал бы поставить CRYPTO, а не & CRYPTO в аргументах ... Для меня CRYPTO - это значение, которое я хочу, поэтому 0xe8ba8fa3 и & CRYPTO адрес.И для меня, CRYPTO не указатель, так почему мне нужно использовать memcpy с & CRYPTO, чтобы он работал?
Кстати, мой код может быть просто катастрофой, я новичок.Не стесняйтесь меня поправлять!
Спасибо!