"googler" <pi********@yahoo.com> writes:
Thanks everybody for the help. > myobject.b = (char *)malloc(50);
As Every Body Said Not Need To Type Cast.
Doesn't K&R use type cast with malloc()? Maybe it's not a good
practice, but I'm wondering that in almost all C codes I've seen, type
cast has been used with malloc().
Yes, but the errata list at
<http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html> corrects it:
142(6.5, toward the end): The remark about casting the return
value of malloc ("the proper method is to declare ... then
explicitly coerce") needs to be rewritten. The example is correct
and works, but the advice is debatable in the context of the
1988-1989 ANSI/ISO standards. It's not necessary (given that
coercion of void * to ALMOSTANYTYPE * is automatic), and possibly
harmful if malloc, or a proxy for it, fails to be declared as
returning void *. The explicit cast can cover up an unintended
error. On the other hand, pre-ANSI, the cast was necessary, and it
is in C++ also.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.