这是我的代码的示例:
/* 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配合使用才能使其正常工作?
顺便说一句,我的代码可能纯粹是灾难,我是一个初学者。不要犹豫,纠正我!
谢谢!
答案 0 :(得分:6)
我的问题是,为什么最后一个memcpy起作用?我希望 将CRYPTO而不是&CRYPTO放在参数中...对我来说,CRYPTO是值 我想要0xe8ba8fa3和&CRYPTO地址。
您是对的。 CRYPTO
不是指针。但是,memcpy需要一个指针,因此我们必须给它一个指针。我们通过获取CRYPTO
的地址来完成此操作,并通过在其上添加&
来实现,因此也就是&CRYPTO
。
memcpy
所做的是将内存从一个地址复制到另一个地址(这就是为什么要使用两个指针),而不管这些地址上的实际内容如何。如果您给它CRYPTO
而不是它的指针,它很可能会将CRYPTO
的值解释为一个地址(行为是不确定的,除非编译器给出一个,否则无法保证会发生什么)
答案 1 :(得分:1)
memcpy function notifyMe(text) {
if (Notification.permission !== "granted")
Notification.requestPermission();
else {
var notification = new Notification('Notification title', {
body: text,
requireInteraction: true
});
notification.addEventListener("click", function (event) {
alert("onClick!");
event.preventDefault();
console.log('Notification clicked.');
} )
notification.addEventListener("close", function (event) {
alert("onClose!");
event.preventDefault();
console.log('Notification clicked.');
} )
}
}
参数