Arrays (C++)

From RAD Studio
(Redirected from Arrays)
Jump to: navigation, search

Go Up to Language Structure Index


The declaration:

type declarator [<constant-expression>]

declares an array composed of elements of type. An array consists of a contiguous region of storage exactly large enough to hold all of its elements.

If an expression is given in an array declarator, it must evaluate to a positive constant integer. The value is the number of elements in the array. Each of the elements of an array is numbered from 0 through the number of elements minus one.

Multidimensional arrays are constructed by declaring arrays of array type. The following example shows one way to declare a two-dimensional array. The implementation is for three rows and five columns but it can be very easily modified to accept run-time user input.

/* DYNAMIC MEMORY ALLOCATION FOR A MULTIDIMENSIONAL OBJECT. */
// ---------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <vcl.h>
#pragma hdrstop

#include <tchar.h>
// ---------------------------------------------------------------------------

#pragma argsused

int _tmain(int argc, _TCHAR* argv[]) {
    double **m;
    const int rows = 3, columns = 4;
    unsigned int i, j;

    /* SET UP THE ROWS */
    m = (double**) calloc(rows, sizeof(double*));

    /* SET UP THE COLUMNS */
    for (i = 0; i < rows; ++i) {
        m[i] = (double*) calloc(columns, sizeof(double));
    }

    /* INITIALIZE */
    for (i = 0; i < rows; i++) {
        for (j = 0; j < columns; j++) {
            m[i][j] = (double)(i + j) / 10.0;
        }
    }

    for (i = 0; i < rows; ++i) {
        puts("");
        for (j = 0; j < columns; ++j) {
            printf("%4.4lf  \t", m[i][j]);
        }
    }

    /* DEALLOCATE */
    /* DELETE EACH COLUMN */
    for (i = 0; i < rows; i++) {
        free(m[i]);
    }
    /* DELETE BASE POINTER */
    free(m);
    return 0;
}
// ---------------------------------------------------------------------------

This code produces the following output:

0.0000 0.1000 0.2000 0.3000
0.1000 0.2000 0.3000 0.4000
0.2000 0.3000 0.4000 0.5000

In certain contexts, the first array declarator of a series might have no expression inside the brackets. Such an array is of indeterminate size. This is legitimate in contexts where the size of the array is not needed to reserve space.

For example, an extern declaration of an array object does not need the exact dimension of the array; neither does an array function parameter. As a special extension to ANSI C, C++Builder also allows an array of indeterminate size as the final member of a structure. Such an array does not increase the size of the structure, except that padding can be added to ensure that the array is properly aligned. These structures are normally used in dynamic allocation, and the size of the actual array needed must be explicitly added to the size of the structure in order to properly reserve space.

Except when it is the operand of a sizeof or & operator, an array type expression is converted to a pointer to the first element of the array.

See Also