Re: Type-casting void pointers?



hugo2 wrote:
Obrhy/hugo July 12, 2004

Take a look at this memcpy() definition.
Is there a good reason the void pointer
args are cast to byte just to assign their
addresses to byte pointers?

/*from Steve Maguire's 'Writing Soild Code'*/

Did you forget an 'e' or get wrong the order of 'i' and 'l'? SCNR

void *memcpy(void *pvTo,void *pvFrom,size_t size)
{
   byte *pbTo = (byte *)pvTo;
   byte *pbFrom = (byte *)pvFrom;
   while(size-- >0)
      *pbTo++ = *pbFrom++;

   return (pvTo);
}

The addresses are all unsigned int. Why not
simply   byte *pbTo = pvTo;   to initialize?

In the case of void*, the cast is completely unnecessary. I do not know the definition of the type "byte" but if it is anything other than a typedef for "unsigned char", I would suggest that you have a look at the wisdom to be found in c.l.c rather than believing this book: Unnecessary casts are a Bad Thing.

Cheers
 Michael
--
E-Mail: Mine is an   /at/ gmx /dot/ de   address.
.



Relevant Pages