Why does "The C Programming Language" book say I must cast malloc? Why does "The C Programming Language" book say I must cast malloc? c c

Why does "The C Programming Language" book say I must cast malloc?


From http://computer-programming-forum.com/47-c-language/a9c4a586c7dcd3fe.htm:

In pre-ANSI C -- as described in K&R-1 -- malloc() returned a char * and it was necessary to cast its return value in all cases where the receiving variable was not also a char *. The new void * type in Standard C makes these contortions unnecessary.

To save anybody from the embarrassment of leaping needlessly to the defence of K&R-2, I asked Dennis Ritchie for an opinion that I could quote on the validity of the sentence cited above from page 142. He replied:

In any case, now that I reread the stuff on p. 142, I think it's wrong; it's written in such a way that it's not just defensive against earlier rules, it misrepresents the ANSI rules.