For the C code below, compare the defintions of the int pointers a and b;
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *a=malloc(sizeof(int));
int *b=(int *)malloc(sizeof(int));
return(0);
}
Is it better in any way to typecast the pointer of type void, returned by the malloc function?
Or is it auto-typecasted while assigning to the int pointer on the left hand side?
Under which circumstances, if any, can it prove to be necessary rather than just obligatory?
Please clarify whether implicit type casting, where type of right hand side is converted to type of the left hand side, applies here.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…