3 ms·
You don't need to cast there... malloc returns a void*.
by icedchai 8y ago
You don't need to cast there... malloc returns a void*.
- AnimalMuppet 8y agoWhich you are going to use how? Don't you have to cast it to something to be able to use it?
- deathanatos 8y agoNo, 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.