On 2008-06-04, Jens Thoms Toerring wrote:
> boyphp wrote:
>> I have a basic question on this code:

>> int main()
>> {
>> char *str;
>> int *num;

>> str = (char *)malloc(20);

> If you're writing C don't cast the return value of malloc(),
> it's useless and will keep the compiler from telling you
> if you forgot to include where malloc() is
> declared.

Yes, where did you (OP) find an example that led you to think you should
cast malloc (and not include )? The author of that example
should be spanked!

Coincidentally, I was just reading this well-written article a day or
two ago explaining why most uses of casts, including casting malloc,
in C is a sign of bad programming:


These bad programming practices need to be extinguished.