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 };
有一个字符数组 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 };