8 ms·
No, you don't. The following, int *i = malloc(sizeof(int)); is valid C, and will compile just fine. I think it's considered good form, too, as the cast do
by deathanatos 8y ago
No, you don't. The following,
int *i = malloc(sizeof(int));
is valid C, and will compile just fine. I think it's considered good form, too, as the cast doesn't really add anything not already present, and might even drift out of date with the surrounding code. (And just gets in the way of legibility.)
IIRC, it is not valid C++. But malloc()/void * is somewhat rarer to find there, as you have better options.
- shakna 8y agoThat's an implicit cast from void * to int * . void *malloc(size_t size);
- caf 8y agoThere's no such thing as an "implicit cast". A cast is an explicit conversion (a cast expression), and the assignment expression discussed here has an implicit conversion.
- tedunangst 8y agoIn standard parlance, that is a conversion. Only (this) is a cast.