Creating a dynamic array of arrays

Hello everybody,

I wanted to implement a card game, but I have to store each person's cards in another array. I don't quite understand why the following code works:

card (players*)[5]=new card[n][5]

but this one doesn't:

card** players = new int[x][5]

Could anyone explain this to me, I'm a bit confused when it comes to multidimensional arrays.

new card[n][5] creates an array of n elements, each of which is an array of 5 elements, each of which is a card. Arrays of arrays are typically referred to as "2D arrays", that is, this creates an nx5 2D array of cards.

the new expression always returns a pointer to the first element of the array that was created: in this case, the first element is an array of 5 cards, and that is exactly what you declare with card (*players)[5].

A pointer to an array of 5 cards is not related to a pointer to a pointer to a card, so the second initialization fails.
Last edited on
Topic archived. No new replies allowed.