1

I want to initialise my matrix to 0, but it does not work because the first part of the matrix is a variable.

int playerCards[playerNum][10] = {0};

I want to make so all the values in the matrix are zero. I have tried the line above, but it tells me that i cannot initialise a the array. For reference, playerNum is an integer between 2 and 6, chosen by the user.

furiosa
  • 49
  • 4

1 Answers1

3

You can use memset():

int playerCards[playerNum][10];
memset(playerCards, 0, sizeof playerCards);

sizeof works for VLAs: in this case playerCards is evaluated, which does not make a difference, but also means that the size is computed at runtime as sizeof(int) * playerNum * 10 using the value of playerNum at the time of instantiation of playerCards.

chqrlie
  • 131,814
  • 10
  • 121
  • 189