Scott wrote:
>Years ago, I learned this trick from somebody in the C/C++ Journal, or
>whatever that rag was called:
>
> memcpy(*data_buf,bp,len)[len] = '\0';
>
>I get the following error:
>
>indatax.c: In function `read_datum':
>indatax.c:374: warning: dereferencing `void *' pointer
>indatax.c:374: invalid use of void expression
My first question would be what is the type of `data_buf'?
Secondly, as others have mentioned, the prototype for memcpy is:
void* memcpy(void* dest, const void* src, size_t len);
so you need to supply the address of a source buffer and the address
of a destination buffer. The statement `*data_buf' can be thought of
as saying "get the value stored in the memory location pointed to by
data_buf" (i.e. dereference data_buf).
>From looking at your example, I take that what you are doing is copying
data in `bp' to the buffer `data_buf' and then appending a null-terminator
to `data-buf'. Assuming that data_buf and buf are properly declared and
defined, the following should work:
((char*)memcpy((void*)data_buf, (const void*)bp, len))[len];
While your `trick' works, it probably is a maintenance nightmare - the
intent is not immediately clear. I would probably do the same thing in
two steps:
memset((void*)data_buf, '\0', len); // fills data_buf with
nulls...
memcpy((void*)data_buf, (const void*)bd, len-1); // copy the data...
Hope this helps,
George
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming"
in
the body of a message to [EMAIL PROTECTED]
More majordomo info at http://vger.kernel.org/majordomo-info.html