Re: cast in memcpy()

From: Martin Dickopp (expires-2004-04-30_at_zero-based.org)
Date: 03/01/04


Date: Mon, 01 Mar 2004 11:41:27 +0100

Greg Barron <greg.barron@bigfoot.NOGARBAGE.com> writes:

> On Sun, 29 Feb 2004 20:14:52 -0800, Ben Pfaff <blp@cs.stanford.edu> wrote:
>
>> root@zworg.com (Amarendra GODBOLE) writes:
>>
>>> 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);
>>
>> Your informant is wrong. No such casts are necessary in C.
>
> To be more specific, no casts are required to convert any pointer type
> to or from void *.

That only applies to pointers to objects, but not to function pointers.
The latter cannot be converted to `void *' (or vice versa) at all.

Martin

-- 
   ,--.    Martin Dickopp, Dresden, Germany                 ,= ,-_-. =.
  / ,- )   http://www.zero-based.org/                      ((_/)o o(\_))
  \ `-'                                                     `-'(. .)`-'
   `-.     Debian, a variant of the GNU operating system.       \_/


Relevant Pages

  • Re: regarding free function in c library
    ... type void* and when we invoke them with pointers to any type, ... No, C does not support "implicit casts", no such thing exists in the ...
    (comp.lang.c)
  • Re: void pointer arithmetic
    ... As for doing pointer arithmetic on the results: If all the pointers ... static int str_qcmp(const void *a, ... If casts are not no-ops, ... receives struct foo pointers, so it doesn't convert them from void* to ...
    (comp.lang.c)
  • Re: [PATCH] remove some usesless casts
    ... > I think Jörn means that if you need an opaque data type, ... casts are a Bad Idea. ... In some cases, this comes down to void pointers, yes. ...
    (Linux-Kernel)
  • Re: Is C type checking?
    ... > If you want to deliberately defeat type-checking, ... And pointers can be converted to and from void * without a ... > void *, thus providing a certain amount of genericity). ... Casts[*] can ...
    (comp.lang.c)
  • Re: cast in memcpy()
    ... > Your informant is wrong. ... No such casts are necessary in C. ... no casts are required to convert any pointer type to ... or from void *. ...
    (comp.lang.c)