0

Can anyone clearly explain (with examples) why do we have to change the data type of a dynamic allocation pointer from the default void type into the same data type of its corresponding pointer variable?

ex:

int *ptr = (int *)malloc(sizeof(int));
user8267163
  • 43
  • 1
  • 6
  • 1
    [We don't and we shouldn't](https://stackoverflow.com/q/605845/69809). – vgru Jul 31 '17 at 09:02
  • Answer is in the duplicate, which will reveal to you that the assumption in your question is not correct. In short, **don't** cast in C, in C++ you need to. – Bathsheba Jul 31 '17 at 09:02
  • 3
    @Bathsheba: in C++ you first need to wonder why you're using malloc :-) – Mat Jul 31 '17 at 09:05

0 Answers0