[This ``longer explanation'' was provided by Mark Brader.]
In early C,
[casts on malloc's return value]
were not used.  malloc() returned a char *
value and this could be implicitly converted, as part of the =
operator, to the desired pointer type.  (In fact, in very early
C, there was no cast operator and all conversions were implicit.)
Later on, implicit conversions of pointer types were seen as
a bad thing, and the language was changed so that an = operator
involving pointers needed the same type on both sides.  Thus all
pointer conversions now required an explicit cast.  (In some
compilers, the old-style implicit conversions still worked, but
a warning would occur.)
Still later, with the coming of ANSI C, the type void * was
introduced, malloc() was changed to return that type, and
implicit pointer conversions went back into the language when
void * was involved.
Therefore, the seemingly redundant casts are used by people who are
- (a)
concerned with portability to all pre-ANSI compilers, or
 - (b)
of the opinion that implicit conversions are a bad thing.
 
back
about this FAQ list
 
about eskimo
 
search
 
feedback
 
copyright
Hosted by