Mail Archives: djgpp/1997/03/03/05:50:47
>Why the HELL do people keep writing things like (char *)malloc(x)????????
>
>GCC, and to my knowledge other compilers, allow any pointer to be assigned
>to a variable of type void * and conversely allow any variable of type void
>* to be assigned to any pointer, without casts.
>
`malloc' returns void* while p can be of any pointer type. You are actually
casting from (void*) to (char*).
In C it is not a problem, the compiler does the conversion for you with or
without warnings (depends on the compiler and warning level).
In C++ it is a compilation error so you must explicitly cast from one
pointer type to another (except when the target type is void*).
Eyal.
- Raw text -