tutorial: handling two dimensional arrays in C18


In previous tutorials, it was explained how to use pointers to handle arrays or variables, and specifically for transferring an array as a function parameter.

In the project of Bolt system controlling RFID tags, we developed a practical application of handling one dimensional arrays in C.

In this tutorial you will see how to handle two dimensional arrays using C18 language. The examples were developed with C18 v.3.40 Compiler and MPLAB IDE v.8.63. We will use the MPLAB IDE simulator to facilitate the undersanding of this topic, and finally an application program using the Bolt 18F2550 system with an LCD will be explained to show the management of two dimensional arrays.

Suppose you want to create an array with 35 data, organized in 5 rows of 7 data each. Consider the example in the table below, where data are defined in ASCII, decimal and hexadecimal formats. The matrix would take the following form:


where i is the row number and j the column number. For example: matriz[4,1]=0XA4

i / j

0 1 2 3 4 5 6
0 'R' 59 'B' 6 0X2E 'T' -81
1 182 'M' -10 'T' 66 0X16 'S'
2 'C' 0XF 'R' 234 118 -30 0XB
3 -52 12 0X45 -101 'R' 0XE1 92
4 -87 0XA4 'Y' 'W' 0 81 0X20

Handling two dimensional arrays in C:

In C, it is essential the use of pointers to reference all array elements. The figure below shows the data structure for the proposed matrix in the previous example.

One dimensional arrays s0[ ] ... s4 [ ] each contain 7 values ​​that makes finally 35 items of the matrix in the proposed example. The general structure of the matrix is shown below:

There is an array of pointers in this example call "masterarray[ ]". To define this array in the program we proceed as follows:

const rom char *masterarray[ ]={s0,s1,s2,s3,s4};

S0...S4 pointers are 16 bit values ​​that are stored in RAM memory locations. But its content is pointing to addresses in ROM memory (code) which is where the elements of the matrix are stored. When performing the simulation test program, shown in the window below, can be seen the values ​​of these pointers in RAM.

To access individual array elements of the matrix, indirect addressing should be used, ie with subscripts added to the value of the pointers.


Based on the structure shown above, the following program defines first data of the matrix of 5 rows and 7 columns that are stored in memory and later uses a function that retrieves the element [4,1] of the matrix. The function used in the program to read the indicated item is:


Where the element value is assigned to the variable z. This function can be used in any other program for handling two dimensional arrays, having as input parameters values ​​[i, j] (row, column) of the matrix.

Test program for simulation is the following: C18-BOLT-TWO-DIMENSIONAL-ARRAY-2.c

To make the simulation, you must first compile the program, and assign a breakpoint on the line shown in the window below (red circle). Reset the simulator program and then click the option "run".

The simulation will stop at the breakpoint (green arrow). The variable z and pointers S0 ... S4 can be seen in the watch window of variables and RAM. In this test example, variable z takes the value of the matrix element [4,1], which is 0xA4.



C18-BOLT-TWO-DIMENSIONAL-ARRAY-2.c Test program 1. Reads element [4,1] of a matrix of 35 elements. For simulation in MPLAB IDE
C18-BOLT-TWO-DIMENSIONAL-ARRAY-3.c Test program 2. Read one by one each of the 35 elements of the matrix. Use breakpoints to simulate in MPLAB
C18-BOLT-TWO-DIMENSIONAL-ARRAY-LCD.c Application program for Bolt 18F2550 system. Writes in LCD the names of 8 persons stored in a group of arrays.
C18-BOLT-TWO-DIMENSIONAL-ARRAY-LCD.hex Executable file of application.