0

I found on google that If you want to dynamic alocate a 2d array in C you need to use this piece of code

    int **arr = (int **)malloc(r * sizeof(int *)); 

I want to ask if and why is really necessary to cast the malloc to (int **). I tested without the (int **) cast and it worked just fine for me, but everywhere on the internet I found that you should use the cast and I don't understand why. Thanks

MihaiM
  • 115
  • 4
  • 2
    You shouldn't cast it. Perhaps the confusion stems from the fact that the code is also valid in C++, a different language in which this line of code only works with the cast. – Blaze Dec 14 '18 at 12:58
  • @Blaze Ok, thanks. I found on google code that on 1d array didn't use the cast and on 2d array used the cast. I think that the code for 2d array was in C++ not C – MihaiM Dec 14 '18 at 13:03

0 Answers0