I donno if this is possible or not but am confused. Are they both same? I know that in the first case we are allocating memory dynamically for 20 elements of type int.
int *p;
p=(int *) malloc(20*sizeof(int));
and
p=(int *) malloc(sizeof(int)*20);
Recall that
sizeofoperator returns type:size_t.In the case of
int * size_t(20*sizeof(int)) versussize_t * int(sizeof(int) * 20): the product is the same as the narrower type is widened to the other first and multiplication in C is commutative:a*bequalsb*a.The situation changes with
int * int * size_tversussize_t * int * intas in C, multiplication is not associative. The first multiplesint * int(with anintproduct), then doesint * size_t. With select values, the first multiplication may overflow whereassize_t * int * intdoes not.When more than 2 objects may be multiplied, best to make certain the widest multiplication happens first and subsequently: example
size_t * int * int.With only 2 objects to multiply, code in whatever way you like.
I like to lead with the likely wider type.
Since the cast is not needed and sizing to the object is easier to code right, review and maintain, consider: