Reputation: 13
I'm getting confused while trying to make the tic tac toe game in c. I'm following a book that uses a single char array of 8 elements to create the board. I know an array[8] contains 9 elements, but if the array is declared as char array, since it has to contain spaces to make the empty board for the game, shouldn't it be array[9], since it has to contain the 9 characters plus a null character '\0'? I'm having the same doubt when converting the board to a 2d array, since I understand the board should be made with an array[3][3], but if I want to declare it as char, where is the place for the null character?
Upvotes: 0
Views: 1816
Reputation: 3672
An array of characters can hold any value in any position. It's only when you want to use certain functions that you need to make sure that there's a NUL character (that's a 0
, not a '0'
!) at the end of the string - that tells the function when to stop processing!
So: char john[4] = { 'J', 'o', 'h', 'n' };
is perfectly legal - just don't pass john
to strlen()
, since it will keep counting until it finds a 0
value, and there aren't any in the array!
Also: char tictactoe[3][3];
doesn't need a 0
anywhere - unless you try to pass the whole array to printf()
for some reason...
Note that you can safely do the following:
printf(" %c | %c | %c \n", tictactoe[0][0], tictactoe[0][1], tictactoe[0][2]);
printf("---+---+---\n");
printf(" %c | %c | %c \n", tictactoe[1][0], tictactoe[1][1], tictactoe[1][2]);
printf("---+---+---\n");
printf(" %c | %c | %c \n", tictactoe[2][0], tictactoe[2][1], tictactoe[2][2]);
because using %c
means "print one character", not "print a string of characters".
The form of a string that ends in a NUL is often called an ASCIIZ (ASCII-Zeroed) string, and is the reason why a nine-char array can only hold an eight-character string (the opposite of what you said) - it needs the ninth to hold the NUL.
And, as always in C, any array goes from 0 to length-1, hence the [0]
, [1]
and [2]
in my example
Upvotes: 1
Reputation: 311146
The terminating zero is needed if a character array is used to store strings.
In the case of your task there is no need to store strings in the array.
So in C you may declare for example a 3*3 array the following way
char board[3][3] =
{
" ", // three spaces
" ",
" "
};
Each element of the array will not contain the terminating zero though it is initialized by a string literal.
On the other hand you can include the terminating zero in each element if you think that it will be simpler to output such an array. In this case the array should be defined like
char board[3][4] =
// ^^^
{
" ", // three spaces
" ",
" "
};
As for your statement that
an array[8] contains 9 elements
then the array is declared as having exactly eight elements. :)
I think you mean string literals. For example string literal "Hi"
in C has type char[3]
due to the terminating zero that is stored in the literal.
Upvotes: 0
Reputation: 133669
Using a character as the value of an array has nothing to do with the fact that an array of char
can decay to a pointer char*
and be interpreted as a NUL
terminated string.
A string doesn't have a specific type in C, it's just a way to interpret some data. So you are mixing two concepts.
A char
array doesn't have to be NUL
terminated, it requires NUL
termination if you are going to use it with functions which will use the data as a string.
Having a char board[3][3]
is perfectly legal and has room for 9 char elements, it doesn't require \0
since you are not going to use that data as a string.
Upvotes: 0
Reputation: 134396
I know an array[8] contains 9 elements
No, why it should be? An array, defined like int arry[8]
contains 8 elements only. That too, in C, array indexing is 0 based, means, for an array as above, only array[0]
to array[7]
will be valid access.
That said, for a char
array, is to be used as string, should have the space for a null-terminator, too. So, if you want to have a char
array of 9 elements and want to use it as a string, you need to define it as char[10]
.
Upvotes: 0
Reputation: 1159
2D arrays are essentially 1D arrays with added ease of use for the programmer. The book you read probably just stores the values at cells, and displays them instead of a chunk of characters as string, like you said. This is a bit more efficient than storing the entire board as string.
Suppose you want to store a 2D array of 4x4 area. You can represent it as a 1D array of size 16, and access indicies like so:
arr[j + i * 4]
Where j is where you are in that 4 unit block, and i is what 4 unit block you are in.
Also, an array of size [8] has 8 elements. Ex:
char arr[8]; // has 8 elements, not 9
Upvotes: 0