Technical Training
C TutorialsC Multidimensional Arrays
C Programming - Arrays
Multidimensional Arrays
Arrays can have more than one dimension. Two dimensional arrays are widely used for tables, neural networks and etc. You can have as many dimensions as you would like but you have to consider the complexity of the code as you add new dimension. Multidimensional arrays allow you to store data in a spreadsheet or matrix like format.
Declaring Multidimensional Arrays
To declare a multidimensional array:
- <data type> array_name[size_of_first_dimension][size_of_second_dimension] ...
A very good example is the creation of 2D maps for games.
- char game_map[10][10];
This would declare a 2 dimensional array of chars that is 10 columns by 10 rows in size.
You could even do a 3D map for a game using a 3 dimensional array.
- char game_map[10][10][5];
You can consider above array as being 10 tiles by 10 tiles on the horizontal plane, and 5 tiles high on the vertical plane.
Initializing Multidimensional Arrays
Just like single dimension array, you can initialize the multidimensional array also upon declaration as well as initialize by assignment.
- <data type> array_name[size_of_dimension1][size_of_second_dimension2] ... = {
- {element 1, element 2, element 3, …},
- .
- .
- . }
- };
- char game_map[2][4];
| array [Y][X] game_map[2][4]; |
X direction ► | |||
| Y direction▼ | game_map[0][0] | game_map[0][1] | game_map[0][2] | game_map[0][3] |
| game_map[1][0] | game_map[2][1] | game_map[3][2] | game_map[4][3] | |
- char game_map[2][4] = { {'x', 'b', 'f', '�'} , {'b', 't', '�' } };
| array [Y][X] game_map[2][4]; |
X direction ► | |||
| Y direction▼ | x | b | f | � |
| b | t | � | empty | |
- int game_map[2][4];
- game_map[0][0]=1; game_map[0][1]=2; game_map[0][2]=3; game_map[0][3]=4
- game_map[1][0]=6; game_map[1][1]=7; game_map[1][2]=8;
| array [Y][X] game_map[2][4]; |
X direction ► | |||
| Y direction▼ | 1 | 2 | 3 | 4 |
| 6 | 7 | 5 | 0 | |
Here is what the above 2D game map array will look like if you decided to initialize it during declaration.
- char game_map[10][10] = {
- {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'},
- {'X', 'H', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
- {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
- {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
- {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
- {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
- {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
- {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
- {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
- {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'}
- };
The first index accesses the rows in the array, the second one accesses the columns. Dimensions more than 2 gets difficult to work with. As far as initializing a 3 or more dimensional array goes, you usually only see them assigned values using a series of nested for loops. Here is a simple example.
- //loop through first dimension
- for (i = 0; i < 10; i++) {
- //loop through second dimension
- for (j = 0; j < 10; j++) {
- //loop through third dimension
- for (k = 0; k < 10; k++)
- int_array[i][j][k]) = i * j * k;
- }
- }
Accessing Elements in Multidimensional Arrays
To access elements in a multidimensional array, all you have to do is provide a combination of index values for each dimension to reach the desired element, just like a bunch of cells in a spreadsheet or coordinates in a n dimensional matrix or graph.
- array_name[dim1_index][dim2_index] ... [dimn-1_index];
Setting values in a multidimensional array is just as simple as in a basic array, you need to add another index per dimension.
- array_name[index1][index2] ... [index s-1] = value;
Let's return to our game_map 2d array for an example of setting a value in a multidimensional array. We could position our hero character in the game_map array using this snippet:
- game_map[1][1] = 't';
That would insert the character 'H' at the intersection of the cell found at
coordinate pair (1, 1) in the array.
Here we will look at retrieving a value from a multidimensional array using the
above syntax. If we wanted to print out our game level map we declared earlier
so that it gets displayed in the console we would use the following group of
nested for loops to go through both dimensions and print every value we
encounter.
- for (i = 0; i < 10; i++) {
- for (j = 0; j < 10; j++) {
- printf("%c", game_map[i][j]);
- }
- }
The first for loop goes through the index values from 0 to 9 of the first dimension of the 2d array. The second for loop does the same for the second dimension. We use the stdio built-in printf function to print the char (“%c”) found at the coordinate pair (i, j). because of the nested loops, this code will print every value in the 2d array game_map out to the console from indexes (0, 0) through (9, 9).
Sample Program
The code provided below defines a level in a treasure hunting game. It shows how to declare and initialize both single and multi-dimensional array. The code shows how to access and index of each element using a for loop.
- //Treasure Hunter
- //Very simple example C programming game.
- //Example snippet for illustrating use of arrays.
- #include <stdio.h>
- void main(void) {
- int i, j;
- char hero = 'H';
- //Declares an array with 2 elements and initializes it.
- char obstacles[3] = {'X', ' ', 'T'};
- //Declare and initialize a 2 dimensional array of 10 rows and 10 columns.
- //The use of obstacles array in the first column is just as an example of how
- //to access the value of an array.
- char game_map[10][10] = {
- {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'},
- {obstacles[0], ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
- {obstacles[0], ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
- {obstacles[0], ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
- {obstacles[0], ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
- {obstacles[0], ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
- {obstacles[0], ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
- {obstacles[0], ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
- {obstacles[0], ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
- {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'},
- };
- //Place our hero at position 1,1 on the map.
- game_map[1][1] = hero;
- //Print the whole 2d array.
- //loop through first dimension
- for (i = 0; i < 10; i++) {
- //loop through second dimension
- for (j = 0; j < 10; j++) {
- //print each array element.
- printf("%c", game_map[i][j]);
- }
- //Add newline at end to ensure each row gets printed correctly.
- printf("n");
- }
- }
When you compile and run the above program, you will see output as shown below:

C Tutorials
- C Programming - An Overview
- C Programming - Data Types : Part 1
- C Programming - Data Types : Part 2
- C Programming - Constants and Identifiers
- C Programming - Operators
- C Programming - Expressions
- C Programming - Managing Input and Output Operations
- C Programming - Decision Making - Branching
- C Programming - Decision Making - Looping
- C Programming - Arrays
- C Programming - Handling of Character String
- C Programming - Functions (Part-I)
- C Programming - Functions (Part-II)
- C Programming - Structures and Unions
- C Programming - Pointers
- C Programming - Dynamic Memory allocation
- C Programming - Linked Lists
- C Doubly Linked Lists
- C Circular Linked Lists
- C Programming - File management in C
- C Language - The Preprocessor
- Call by Value and Call by Reference
- Concept of Pixel in C Graphics
- TSR in C - An Introduction







