Is typecast required in malloc? [duplicate]

I assume you mean something like this:

int *iptr = (int*)malloc(/* something */);

And in C, you do not have to (and should not) cast the return pointer from malloc. It’s a void * and in C, it is implicitly converted to another pointer type.

int *iptr = malloc(/* something */);

Is the preferred form.

This does not apply to C++, which does not share the same void * implicit cast behavior.

Leave a Comment