Arraysand Null-Terminated Strings

 Home

Arraysand Null-Terminated Strings


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. Arrays may have from one to several dimensions. The most common array is the null-terminated string, which is simply an array of characters terminated by a null. 

Single-Dimension Arrays

 The general form for declaring a single-dimension array is
type var_name[size];
Arrays must be explicitly declared so that the compiler may 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;
assigns element number 3 in balance the value 12.23. In C/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 ten elements, p[0] through p[9]. For example, the following program loads an integer array with the numbers 0 through 99:

#include <iostream>
using namespace std;
int main()
{
int x[100]; /* this declares a 100-integer array */ 
int t;.
/* load x with values 0 through 99 */ 
for(t=0; t<100; ++t) x[t] = t;

/* display contents of x */
for(t=0; t<100; ++t){
cout<< x[t];
}
return 0;
}
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:
total bytes = sizeof(base type) x size of array
Single-dimension arrays are essentially lists of information of the same type that are stored in contiguous memory locations in index order. For example, shows how array a appears in memory if it starts at memory location 1000 and is declared as shown here:
char a[7];

Element a[0]  a[1]   a[2]  a[3] a[4]  a[5]  a[6]
Address 1000 1001 1002 1003 1004 1005 1006

Generating a Pointer to an Array

You can generate a pointer to the first element of an array by simply specifying the array name, without any index. For example, given

int sample[10];

you can generate a pointer to the first element by using the name sample. Thus, the following program fragment assigns p the address of the first element of sample:
int *p;
int sample[10];
p = sample;

Null-Terminated Strings 

By far the most common use of the one-dimensional array is as a character string. C++ supports two types of strings. The first is the null-terminated string, which is a null-terminated character array. (A null is zero.) Thus a null-terminated string contains the characters that comprise the string followed by a null. This is the only type of string defined by C, and it is still the most widely used
Sometimes null-terminated strings are called C-strings. C++ also defines a string class, called string, which provides an object-oriented approach to string handling.
When declaring a character array that will hold a null-terminated string, you need to declare it to be one character longer than the largest string that it is to hold. For example, to declare an array str that can hold a 10-character string, you would write
char str[11];
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. C/C++ supports a wide range of functions that manipulate null-terminated strings. The most common are

These functions use the standard header file string.h in c. (C++ programs can also use the new-style header <cstring>.)
Two-Dimensional Arrays

C/C++ supports multidimensional arrays. The simplest form of the multidimensional array is the two-dimensional array. A two-dimensional array is, essentially, an array of one-dimensional arrays. To declare a two-dimensional integer array d of size 10,20, you would write
                 int d[10][20];
Similarly, to access point 1,2 of array d, you would use
d[1][2]
The following example loads a two-dimensional array with the numbers 1 through 12 and prints them row by row.
#include <stdio.h>
int main(void) {
int t, i, num[3][4];

for(t=0; t<3; ++t){ 
    for(i=0; i<4; ++i){
num[t][i] = (t*4)+i+1;
}
}
/* now print them out */ 
for(t=0; t<3; ++t) {
 for(i=0; i<4; ++i){
 cout<< num[t][i]<< endl; // endl is for new line
}
}
return 0;
}

In this example, num[0][0] has the value 1, num[0][1] the value 2, num[0][2] the value 3, and so on. The value of num[2][3] will be 12. You can visualize the num array as shown here:



Two-dimensional arrays are stored in a row-column matrix, where the first index indicates the row and the second 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. See Figure 4-2 for a graphic representation of a two-dimensional array in memory. 

Multidimensional Arrays

 C/C++ allows arrays of more than two dimensions. The exact limit, if any, is determined by your compiler. The general form of a multidimensional array declaration is
type name[Size1][Size2][Size3]. . .[SizeN];
Arrays of more than three dimensions are not often used because of the amount of memory they require. For example, a four-dimensional character array with dimensions 10,6,9,4 requires
                10 * 6 * 9 * 4
or 2,160 bytes. If the array held 2-byte integers, 4,320 bytes would be needed. If the array held doubles (assuming 8 bytes per double), 17,280 bytes would be required. The storage required increases exponentially with the number of dimensions. For example, if a fifth dimension of size 10 was added to the preceding array, then 172, 800 bytes would be required. In multidimensional arrays, it takes the computer time to compute each index. This means that accessing an element in a multidimensional array can be slower than accessing an element in a single-dimension array. When passing multidimensional arrays into functions, you must declare all but the leftmost dimension. For example, if you declare array m as
                int m[4][3][6][5];
a function, func1(), that receives m, would look like this:

void func1(int d[][3][6][5]) {
.
. .
}

Of course, you can include the first dimension if you like.


Comments