Deferred void * pointer during memcpy, want to know how this can be solved?

I am copying src from index buf 5. I got a warning in the src buffer. Just want to know what mistake am I making here?

This is the warning I received. warning: dereferencing void * pointer

uint8_t grx_buf[1024];
const void *buf;
uint8_t len = 12;

memcpy(&grx_buf[grx_count], &buf[5], len);
                                ^ 

      

When I specify this datatype of the destination buffer I got this

memcpy(&grx_buf[grx_count], *(uint8_t*)&buf[5], len);

      

Attention:

note: expected 'const void * is restricted, but the argument is of type' unsigned char

extern void *memcpy (void *__restrict __dest, const void *__restrict __src,

      

+3


source to share


3 answers


You have it almost correct!

memcpy(&grx_buf[grx_count], *(uint8_t*)&buf[5], len);

      

Instead, you must pass pointer variables to arguments src

and dst

memcpy. In that statement, in the second argument (argument src

), you do the following:

  • Call void * to get the 5th item. This is the same error you received the first time.

  • Taking his address.

  • Listing in uint8_t * (what you really want)

  • Dereferencing again, so you have uint8_t instead of a pointer, which is the memcpy expcts function.

What you really want is the following:

memcpy(&grx_buf[grx_count], (uint8_t*)buf+5, len);

      

or, alternatively, trying to follow your syntax:

memcpy(&grx_buf[grx_count], &((uint8_t*)buf)[5], len);

      



  • Passing buf to uint8_t *

  • Take the 5th element of the full type, so you get uint8_t

  • Take its address, so you have a pointer to uint8_t, which is what memcpy expects.


Edit: Note that your buffer is of type const void*

, so all pointer casts must be printed const uint8_t *

instead uint8_t*

to keep the constant constant.

Alternatively, an easier (less verbose) way is to create a new variable:

const uint8_t *buf8=buf; //No cast required, since in C (not C++) `void*` can be implicitly converted to any type.

      

You should now use buf8 like this:

memcpy(&grx_buf[grx_count], buf8+5, len);

      

+4


source


Check data types.

buf

is void *

, and you cannot dereference a pointer that is not a pointer to a complete type. void

is an incomplete type that cannot be completed, so the compiler cannot do any pointer arithmetic on that.

You need to cast buf

to the full type, then track it down. If necessary, you can return the result of the unary &

in again const void *

.




However, the code looks wrong. It has no memory allocation, and indexing on a shared pointer doesn't make much sense either. if void *

used as a generic pointer (or container) then you need to cast it back to its original type before using it.

+2


source


Wherein:

memcpy(&grx_buf[grx_count], *(uint8_t*)&buf[5], len);

      

as the second argument instead of passing the pointer you are looking for. This is why you get this warning:

note: expected 'const void * is restricted, but the argument is of type' unsigned char

The function expects a pointer, but you are passing an unsigned char. Modify the function call as follows:

memcpy(&grx_buf[grx_count], (uint8_t*)&buf[5], len);

      

0


source







All Articles