Friday, 30 June 2017

c - Is typecast required in malloc?

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.

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...