-2

There is a char array ACKbuffer[2] so I first cast to unsigned char the element that I want but it's giving me an error when I try to memcpy it to an unsigned char array.

//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);

Error:

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);

I also tried another way but I get invalid initializer:

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

Ideally, I want my ACK array to hold that value from ACKbuffer.

mLstudent33
  • 1,033
  • 3
  • 14
  • 32
  • 1
    `memcpy(ACK, ACK_actual, 1);` -> `memcpy(ACK, &ACK_actual, 1);`. `ACK_actual` is not a pointer and neither an array that decays to a pointer. Or `unsigned char ACK [1] = {ACK_actual};`. – walnut Feb 09 '20 at 23:57
  • I resorted to using `memset(ACK, ACK_actual, sizeof(ACK_actual));` It compiled but is this wrong? – mLstudent33 Feb 10 '20 at 00:00
  • That works as well, but why make it so complicated? `unsigned char ACK [1] = {ACKbuffer[1]};` works fine since C99. Why do you need the one-element array in the first place? – walnut Feb 10 '20 at 00:07
  • @walnut the checksum function accepts unsigned char arrays as input of varying length. I use the same function to calculate checksum of up to 125 byte long messages as well as 1 byte ACK. – mLstudent33 Feb 10 '20 at 00:11
  • IMHO this smells like using a C++ compiler to compile C. – wildplasser Feb 10 '20 at 00:16
  • @mLstudent33 How did you declare that function? – walnut Feb 10 '20 at 00:18
  • `gcc test_swap_server.c swap_server.c sdp.c -o tester_s` That is the command used to compile. How can I tell if it's for C++ or C? – mLstudent33 Feb 10 '20 at 00:19
  • @walnut `unsigned char checksum(unsigned char message [], int num_bytes)` – mLstudent33 Feb 10 '20 at 00:30
  • @mLstudent33 `unsigned char message []` is a pointer. You can pass `&ACK_actual` directly to it. You are compiling as C, that is not an issue. – walnut Feb 10 '20 at 00:47

1 Answers1

0

In this call of memcpy

memcpy(ACK, ACK_actual, 1);

the variable ACK_actual has the type unsigned char

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

but the function expects a pointer type.

Just write

memcpy(ACK, ACKbuffer + 1, 1);

As for the second code snippet then you should write

unsigned char ACK_actual = (unsigned char)ACKbuffer[1];
unsigned char ACK [1] = { ACK_actual };
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335