casting void pointers
Roger A. Hurwitz
hurwitz at enuxha.eas.asu.edu
Sun Apr 21 03:59:45 AEST 1991
Appendix A, section 6.8, of K&R 2d, states in part,
"Unlike the pointer-to-pointer conversions discussed in [section]
A6.6, which generally require an explicit cast, pointers may
be assigned to and from pointers of type *void, and may
be compared with them."
Does this mean that the explicit cast that one always sees with
malloc() is unnecessary since malloc() returns a void pointer?
For example, why not:
fooptr = malloc(sizeof(struct foo));
instead of:
fooptr = (struct foo *) malloc(sizeof(struct foo));
Even K&R 2d states that an explicit cast should be used with malloc(),
in another part of the book (p. 142), but I am having trouble reconciling
that with the above language.
More information about the Comp.lang.c
mailing list