So I have a sample code to create a deck of card for a mini poker game in c. But I do not understand how the suits and faces are determined. Why do these arrays have 2 dimensions? I know that [9]
and [6]
are the columns of the array, but I do not understand the purpose of them.
char suits[4][9]= {"Hearts","Diamonds","Clubs","Spades"};
char faces[13][6]= {"Ace","2","3","4","5","6","7","8","9", "10","Jack",
"Queen","King"};
The first set of square brackets is the number of elements in the first array, the second square bracket is the maximum length of the char
array (string).
The second bracket in char suits[4][9]
has nine spaces to allow space for the null character \0
which is used to terminate the string.
So the array actually looks like this:
char suits[4][9] = {
{'H', 'e', 'a', 'r', 't', 's', '\0'},
{'D', 'i', 'a', 'm', 'o', 'n', 'd', 's', '\0'},
{'C', 'l', 'u', 'b', 's', '\0'},
{'S', 'p', 'a', 'd', 'e', 's', '\0'}
};