Книга: Advanced PIC Microcontroller Projects in C

3.1.14 Arrays

3.1.14 Arrays

Arrays are used to store related items in the same block of memory and under a specified name. An array is declared by specifying its type, name, and the number of elements it will store. For example:

unsigned int Total[5];

This array of type unsigned int has the name Total and has five elements. The first element of an array is indexed with 0. Thus, in this example, Total[0] refers to the first element of the array and Total[4] refers to the last element. The array Total is stored in memory in five consecutive locations as follows:

Total[0]
Total[1]
Total[2]
Total[3]
Total[4]

Data can be stored in the array by specifying the array name and index. For example, to store 25 in the second element of the array we have to write:

Total[1] = 25;

Similarly, the contents of an array can be read by specifying the array name and its index. For example, to copy the third array element to a variable called Temp we have to write:

Temp = Total[2];

The contents of an array can be initialized during the declaration of the array by assigning a sequence of comma-delimited values to the array. An example follows where array months has twelve elements and months[0] = 31, months[1] = 28, and so on:

unsigned char months[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

The same array can also be declared without specifying its size:

unsigned char months[] = {31,28,31,30,31,30,31,31,30,31,30,31};

Character arrays can be declared similarly. In the following example, a character array named Hex_Letters is declared with 6 elements:

unsigned char Hex_Letters[] = {'A', 'B', 'C', 'D', 'E', 'F'};

Strings are character arrays with a null terminator. Strings can be declared either by enclosing the string in double quotes, or by specifying each character of the array within single quotes and then terminating the string with a null character. The two string declarations in the following example are identical, and both occupy five locations in memory:

unsigned char Mystring[] = "COMP";

and

unsigned char Mystring[] = {'C', 'O', 'M', 'P', ''};

In C programming language, we can also declare arrays with multiple dimensions. One-dimensional arrays are usually called vectors, and two-dimensional arrays are called matrices. A two-dimensional array is declared by specifying the data type of the array, the array name, and the size of each dimension. In the following example, a two-dimensional array named P is created having three rows and four columns. Altogether, the array has twelve elements. The first element of the array is P[0][0], and the last element is P[2][3]. The structure of this array is shown below:

P[0][0] P[0][1] P[0][2] P[0][3]
P[1][0] P[1][1] P[1][2] P[1][3]
P[2][0] P[2][1] P[2][2] P[2][3]

Elements of a multidimensional array can be specified during the declaration of the array. In the following example, two-dimensional array Q has two rows and two columns, its diagonal elements are set to 1, and its nondiagonal elements are cleared to 0:

unsigned char Q[2][2] = { {1,0}, {0,1} };

Оглавление книги


Генерация: 0.594. Запросов К БД/Cache: 2 / 0
поделиться
Вверх Вниз