繁体   English   中英

C memcpy unsigned char 到 1 个元素的 unsigned char 数组用于校验和计算

[英]C memcpy unsigned char to 1 element unsigned char array for checksum calculation

有一个字符数组ACKbuffer[2]所以我先转换为unsigned char元素,我想,但它给我一个错误,当我尝试memcpy它的unsigned char数组。

//first element is checksum
unsigned char ack_csum;
ack_csum = (unsigned char)ACKbuffer[0];
//second element is ACK which needs to be run through checksum 
//in a checksum function that accepts unsigned char array as parameter
unsigned char ACK_actual = (unsigned char)ACKbuffer[1];
unsigned char ACK [1];
memcpy(ACK, ACK_actual, 1);

错误:

note: expected ‘const void * restrict’ but argument is of type ‘unsigned char’
 extern void *memcpy (void *__restrict __dest, const void *__restrict __src,
              ^~~~~~
swap_client.c: In function ‘swap_write’:
swap_client.c:185:17: warning: passing argument 2 of ‘memcpy’ makes pointer from integer without a cast [-Wint-conversion]
     memcpy(ACK, ACK_actual, 1);

我也尝试了另一种方式,但我得到了invalid initializer

unsigned char ACK_actual = (unsigned char)ACKbuffer[1];
unsigned char ACK [1] = ACK_actual;

理想情况下,我希望我的ACK数组保存来自ACKbuffer值。

在这个 memcpy 调用中

memcpy(ACK, ACK_actual, 1);

变量 ACK_actual 的类型为 unsigned char

unsigned char ACK_actual = (unsigned char)ACKbuffer[1];

但该函数需要一个指针类型。

写就好了

memcpy(ACK, ACKbuffer + 1, 1);

至于第二个代码片段,那么你应该写

unsigned char ACK_actual = (unsigned char)ACKbuffer[1];
unsigned char ACK [1] = { ACK_actual };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM