For making a tictactoe game, I decided that the optimal solution will be to create an array of arrays. I have found solutions only for C# and C++, and I am not sure if this option exists in C. I wonder if I can use some libraries available for C to create an array of arrays.
The code I’m trying to execute:
char first_row[] = {' ', ' ', ' '};
char second_row[] = {' ', ' ', ' '};
char third_row[] = {' ', ' ', ' '};
char rows[] = {first_row, second_row, third_row};
I’d also like to know how to access an array inside an array. A pythonist myself, I find the absence of this feature completely blowing my mind.
upd: I have found information that it’s possible in C99, however, no description of how to make it.
>Solution :
To create a two-dimensional array in C, use the following,
char rows[][3] = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
Note that the dimension 3 is mandatory here and cannot be omitted.
You can also do the following, but then your array is not a two dimensional array, it is an array of pointers instead.
char *rows[] = {first_row, second_row, third_row};
In both cases, you access your element with the same syntax like rows[0][0].