Re: Cast abuse



On Mon, 19 November 11:32 Carlos Morgado wrote:
> 
> On 2001.11.19 11:18:21 +0000 Brian Stafford wrote:
> 
> 
>> all a cast silences the compiler about potential coding errors.  Also 
>> remember that void pointers eliminate the need for a lot of casts, e.g. 
>> code like "ptr = (int *) malloc(..." is always unnecessary since malloc 
>> returns a void *.  Just write "ptr = malloc(...".  The same goes for
> is this C9x ? i certainly remember ansi/k&r compilers didn't allow for
> automagic void * promotion --

AFAIK, this has been the case ever since the introduction of void to K&R C; 
one of the motivating factors for void * was to avoid the need for casting 
char * to the required type when it was used as a generic pointer type.  I 
certainly have never come across a compiler that requires the cast, ANSI or 
K&R+void.  Not even any of the M$ C compilers, despite the M$ penchant for 
polluting code with casts at every opportunity.

Indeed, if a void pointer could not be freely assigned or compared, it would 
not offer any advantage over the use of char pointer as a generic pointer type.

Brian



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]