lect4
lect4
An array is a collection of variables of the same type that are referred to through a
common name. A specific element in an array is accessed by an index. In C, all arrays
consist of contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element. Arrays can have from one to
several dimensions. The most common array is the string, which is simply an array of
characters terminated by a null.
The general form for declaring a single-dimension array is
type var_name[size];
Like other variables, arrays must be explicitly declared so that the compiler can allocate
space for them in memory. Here, type declares the base type of the array, which is the type
of each element in the array, and size defines how many elements the array will hold. For
example, to declare a 100 element array called balance of type double, use this statement:
double balance[100];
An element is accessed by indexing the array name. This is done by placing the index of the
element within square brackets after the name of the array. For example,
balance[3] = 12.23;
In C, all arrays have 0 as the index of their first element. Therefore, when you write
char p[10];
you are declaring a character array that has 10 elements, p[0] through p[9].
The amount of storage required to hold an array is directly related to its type and size.
For a single dimension array, the total size in bytes is computed as shown here:
C has no bounds checking on arrays. You could overwrite either end of an array and
write into some other variable's data or even into the program's code.
Single-dimension arrays are essentially lists that are stored in contiguous memory
locations in index order. For example, Figure shows how array a appears in memory if it
starts at memory location 1000 and is declared as shown here:
char a[7];
Searching: Linear Search
Binary Search
Binary Search
Binary Search
Binary Search
Binary Search
Sorting: Bubble Sort
Two-Dimensional Arrays
int d[10][20];
Two-dimensional arrays are stored in a row-column matrix, where the left index indicates the
row and the right indicates the column. This means that the rightmost index changes faster
than the leftmost when accessing the elements in the array in the order in which they are
actually stored in memory.
In the case of a two-dimensional array, the following formula yields the number of bytes of
memory needed to hold it:
Therefore, assuming 4-byte integers, an integer array with dimensions 10,5 would have
Two-Dimensional Arrays
By far the most common use for the one-dimensional array is as a character string. In C, a
string is a null-terminated character array. (A null is zero.) Thus, a string contains the
characters that make up the string followed by a null. The null terminated string is the only
type of string defined by C.
When declaring a character array that will hold a string, you need to declare it to be one
character longer than the largest string that it will hold. For example, to declare an array str
that can hold a 10-character string, you would write
char str[11];
Specifying 11 for the size makes room for the null at the end of the string.
When you use a quoted string constant in your program, you are also creating a null-
terminated string. A string constant is a list of characters enclosed in double quotes. For
example:
''hello there"
You do not need to add the null to the end of string constants manually— the compiler
does this for you automatically.