0

So I have a sample code to create a deck of card for a mini poker game in . 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"};
Marievi
  • 4,951
  • 1
  • 16
  • 33
Quan Bui
  • 175
  • 1
  • 13
  • 1
    9 and 6 are just the (maximum) string sizes (allowing +1 for the string terminator). 4 and 13 are the numbers of suits and ranks. May I recommend choosing something from [this list of good books on C](http://stackoverflow.com/q/562303/253056) ? – Paul R Apr 27 '16 at 07:36
  • Think about what the type of `"Hearts"` is vs what is the declared type of elements of `suits`? – tobyodavies Apr 27 '16 at 07:37
  • 4
    Personally, I would have just `const char *suits[] = {....};` and eliminated the chance of someone entering in too-small a value for the inferior dimension. That, and I see little reason anyone would need to *modify* those strings during the runtime of the program. – WhozCraig Apr 27 '16 at 07:40
  • @Paul R Ahh! I thought that they were columns for the array for some reason. How silly! Thanks a lot, this make a lot more sense now. – Quan Bui Apr 27 '16 at 07:43
  • Well they sort of *are* columns, if you think of each string as a row, and the characters within each as having a column index. As @WhozCraig says above though, this is a terrible way to declare arrays of string constants. – Paul R Apr 27 '16 at 07:44
  • @PaulR what happens if I don't specify the string length? What will be the maximum number of characters that I can place into each row of the array? – Quan Bui Apr 27 '16 at 07:49
  • If you don't specify a length you could end up getting a segmentation fault, when trying to access invalid memory. – OrderAndChaos Apr 27 '16 at 09:00
  • @JasonBui: if you use the method described by WhozCraig above then you won't have to worry about explicit string lengths. – Paul R Apr 27 '16 at 09:47
  • Could you let me know if you got what you were looking for from my answer. Is there anything that you would like me to clarify? – OrderAndChaos Apr 28 '16 at 19:26
  • @sarcoma Sorry about the late reply! Your answer was really helpful, it covered everything that I was looking for! I tried to vote up but I could not publicly do that since I don't have enough reputation on Stack! Thanks so much for your help! – Quan Bui May 05 '16 at 07:54
  • No problem, glad to help. You should be able to mark it as correct, that is if you agree that it is correct :) – OrderAndChaos May 05 '16 at 12:25

2 Answers2

7

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'}
};
OrderAndChaos
  • 3,547
  • 2
  • 30
  • 57
1

When you have :

char suits[4][9]

it means that your array has 4 rows and the string that will be placed in each row can have a maximum length 9 and subtracting the ending '\0' character, maximum length 8.

Similarly,

char faces[13][6]

means that your array has 13 rows and the string that will be placed in each row can have a maximum length 6 and subtracting the ending '\0' character, maximum length 5.

Marievi
  • 4,951
  • 1
  • 16
  • 33