Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Inserting an array into an array

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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].

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading