Hardware Reference
In-Depth Information
To access the value pointed to by a pointer, use the dereferencing operator *. For example,
int a, *b;
// b is a pointer to int
. . .
a 5 *b;
assigns the value pointed to by b to variable a .
We can assign the address of a variable to a pointer by using the unary operator &. The fol-
lowing example shows how to declare a pointer and how to use & and *:
int x, y;
int *ip;
// ip is a pointer to an integer
ip 5 &x;
// assigns the address of the variable x to ip
y 5 *ip;
// y gets the value of x
5.7.2 Arrays
Many applications require the processing of multiple data items that have common charac-
teristics (e.g., a set of numerical data, represented by x 1 , x 2 , . . . , x n ). In such situations it is more
convenient to place data items into an array, where they will all share the same name. The
individual data items can be characters, integers, floating-point numbers, and so on. They must
all be of the same type and the same storage class.
Each array element is referred to by specifying the array name followed by one or more
subscripts, with each subscript enclosed in brackets. Each subscript must be expressed as a
nonnegative integer. Thus, the elements of an n -element array x are x [0], x [1], . . . , x [ n - 1]. The
number of subscripts determines the dimensionality of the array. For example, x[i] refers to an
element of a one-dimensional array. Similarly, y[i][j] refers to an element of a two-dimensional
array. Higher-dimensional arrays can be formed by adding additional subscripts in the same
manner. However, higher-dimensional arrays are not used very often in 8- and 16-bit microcon-
troller applications. In general, a one-dimensional array can be expressed as
data-type array_name[expression];
A two-dimensional array is defi ned as
data-type array_name[expr1][expr2];
An array can be initialized when it is defi ned. This is a technique used in table lookup, which
can speed up the computation process.
Example 5.7
Write the bubble sort function to sort an array of integers.
Solution: The algorithm for bubble sort is already described in Chapter 4. Here is the C language
version.
void
swap (int *px, int *py);
void
bubble (int a[], int n) /* n is the array count */
{
int i, j;
for (i 5 0; i , n 2 1; i 11 )
for (j 5 0; j . n 2 i 2 2; j 11 )
 
Search WWH ::




Custom Search