Mikroc – ABL electronic PIC Microcontrollers PIC16 User Manual

Page 75

Advertising
background image

Multi-dimensional Arrays

An array is one-dimensional if it is of scalar type. One-dimensional arrays are
sometimes referred to as vectors.

Multidimensional arrays are constructed by declaring arrays of array type. These
arrays are stored in memory in such way that the right most subscript changes
fastest, i.e. arrays are stored “in rows”. Here is a sample 2-dimensional array:

float

m[50][20];

/* 2-dimensional array of size 50x20 */

Variable m is an array of 50 elements, which in turn are arrays of 20 floats each.
Thus, we have a matrix of 50x20 elements: the first element is

m[0][0]

, the last

one is

m[49][19]

. First element of the 5th row would be

m[0][5]

.

If you are not initializing the array in the declaration, you can omit the first dimen-
sion of multi-dimensional array. In that case, array is located elsewhere, e.g. in
another file. This is a commonly used technique when passing arrays as function
parameters:

int

a[3][2][4];

/* 3-dimensional array of size 3x2x4 */

void

func(int n[][2][4]) {

/* we can omit first dimension */

//...

n[2][1][3]++;

/* increment the last element*/

}//~

void

main() {

//...

func(a);

}//~!

You can initialize a multi-dimensional array with an appropriate set of values
within braces. For example:

int

a[3][2] = {{1,2}, {2,6}, {3,7}};

MikroElektronika: Development tools - Books - Compilers

67

page

mikroC - C Compiler for Microchip PIC microcontrollers

mikroC

making it simple...

Advertising
This manual is related to the following products: