A
Amarendra GODBOLE
Hi,
I am a bit confused over the correct usage of memcpy(). Kindly help me
clear the confusion.
The linux manpage for memcpy(3) gives me the following prototype of
memcpy(3):
#include <string.h>
void *memcpy(void *dest, const void *src, size_t n);
Now, I have a char array (char arr[256]), and a pointer to a structure
(PSMessage *p). I have to memcpy() the contents of this structure to
the char array. I am doing it like this:
memcpy(arr, p, sizeof *p);
Someone tells me -- this is an incorrect usage, you either need to do
memcpy(arr, (char *)p, sizeof *p);
or
memcpy((void *)arr, (void *)p, sizeof *p);
Do I need the cast here ? Or the way I am trying to do it is correct ?
I have not tested the code, so I am not sure of it either.
Thanks in advance.
Cheers,
Amarendra
I am a bit confused over the correct usage of memcpy(). Kindly help me
clear the confusion.
The linux manpage for memcpy(3) gives me the following prototype of
memcpy(3):
#include <string.h>
void *memcpy(void *dest, const void *src, size_t n);
Now, I have a char array (char arr[256]), and a pointer to a structure
(PSMessage *p). I have to memcpy() the contents of this structure to
the char array. I am doing it like this:
memcpy(arr, p, sizeof *p);
Someone tells me -- this is an incorrect usage, you either need to do
memcpy(arr, (char *)p, sizeof *p);
or
memcpy((void *)arr, (void *)p, sizeof *p);
Do I need the cast here ? Or the way I am trying to do it is correct ?
I have not tested the code, so I am not sure of it either.
Thanks in advance.
Cheers,
Amarendra