In C and C++, the array index (i.e., subscript) is enclosed in square brackets (as in Pascal), e.g.,
a[i]The convention of C/C++ is that the array index value always begins at zero and this initial value cannot be changed.
In C/C++, the array name itself can be considered to be a pointer variable (to be discussed in greater depth later), which stores the address of the first element of the array. Thus a[0] is the contents of the first element in array a, while a itself is the "address," i.e., memory location, of that first element.
Thus the subscript can also be considered to be the "offset" from the starting location ("base location"). If the subscript is 0, there is no offset, and thus, a[0] refers to the contents of the starting location.
FORTRAN-77 Style
REAL A(50) INTEGER NUM(10) REAL B(5:10), C(-5:5)A is an array of type REAL numbers containing 50 variables. NUM is an array of type INTEGER numbers containing 10 variables. B contains 6 variables and C contains 11 variables.
The total number of variables in an array is
high_subscript - low_subscript + 1
Fortran-90/95 Style
REAL, DIMENSION(1:50) :: A INTEGER, DIMENSION(1:10) :: NUM REAL, DIMENSION(5:10) :: B REAL, DIMENSION(-5:5) :: C
(For more information, follow this link.)
C++ Arrays C++ arrays are declared merely by indicating the total number of elements in the array after the array name in a type declaration statement. For example,
int a[10]; float b[25];The array a has space allocated for 10 integer variables. NOTE, however, that the legal subscripts go from 0 to 9 and do not include the number 10! Similarly, b has space allocated for 25 real variables, with subscripts from 0 to 24.
(For more information, follow this link.)
FORTRAN two (or higher) dimensional arrays are declared as follows:
REAL A(2,3), B(5,3,10) INTEGER NUM(2:4,-2:2)or, in Fortran-90/95 style,
REAL, DIMENSION(2,3) :: A INTEGER, DIMENSION(2:4,-2:2) :: NUMWhen using such variables, the subscripts are enclosed in one set of parentheses, e.g., A(1,2).
C++ and C declare array variables as follows:
int a[10][5]; float b[10][20][5];Note the use of separate square brackets around each subscript dimension. Separate brackets are also used when referring to a particular elements, e.g., a[i][j] or b[0][14][3].
Computing location of C++ array elements
The approach is similar to what is described in Essentials for Fortran arrays, with these differences: C++ stores by row, and not by column, and the "correction" of "-1" in the formula is not needed because C++ subscripts start at zero. The text gives the details on pg. 57.
C++ permits arrays to be allocated in the course of running a program, in a sense, as needed. This is useful for allocating space for arrays whose length is not known until, for example, a certain number is read in from a data file. This is best understood via the concepts of "pointers," most commonly used in linked lists and trees, to be covered later in the course. (For more information on pointers, follow this link.) Since a pointer variable is a variable that stores an address of a memory location, and since the array name by itself is interpreted to be the memory location of the first element of the array, one can understand the array name as a "pointer variable" pointing to the location of the first element of an array.
For future reference, the following shows how to allocate an integer array of size n.
First, one declares a variable to be a pointer to a specific data-type. For example,
int * a;Then, in the course of a program, one can allocate space via the function new and assign it to pointer variable a, as follows:
a = new int[n];This would assign n spaces for array a, with the assumption that at this point in the program, n does have a specific value (a constant may also be used).
After this point, the identifier a may be used as an array as if it were declared in the more standard fashion, i.e., by using a[i] or a[1].
Fortran-90/95 now introduces record variables, calling them "derived types." One defines the time first, giving it a name, and then declares variables of that new data-type. For example, one defines the new type by a statement such as this:
TYPE INFOLINE CHARACTER (LEN = 30) :: NAME CHARACTER (LEN = 50) :: ADDRESS INTEGER :: ZIP INTEGER :: ACCNTNUM END TYPE INFOLINEThen variables of the new type are declared in this way:
TYPE (INFOLINE) :: ACCOUNT, LIST(100), TEMP
To access a field of a record variable, in general, the principles are the same as with C++ or Pascal, except that, instead of using the period (dot), . , as a subdivider, one uses the percent sign, %. For example,
ACCOUNT%ZIP LIST(1)%ACCNTNUM(For more information, follow this link.)
For practicality, when using a derived type in a program, it is probably easier to include the type definition in a MODULE which can be invoked by each program segment that uses variables of that user-defined type. (For more information, follow this link.)
C++ has extended the record feature in C (which called its records a struct, short for structure), by introducing a class. Both data structures may also include functions as well as data fields. The primary difference has been the inclusion of public and private attributes and the default designation in each type of record. By default, every field and function in a class is private, whereas every field and function in a struct is public.
As a sample definition of a new datatype,
class INFOLINE { public: char name[30]; char address[50]; int zip; int accntnum; };Then variables of the new type are declared in this way:
INFOLINE account, list[100], temp;(For more information, follow this link. Also see Notes A for information about including member functions in classes.)
C++ also has included the concept of inheritance in its definition, which includes the ability of one class to "inherit" part of its structure from another class. Inheritance is discussed in Ford and Topp, in Cpt 12, p. 607ff. Information may also be found at this link.
This page is maintained by Dennis C. Smolarski, S.J.
© Copyright 1998, 1999 Dennis C. Smolarski, SJ, All rights reserved.
Last changed: 6 April 1999.