C++ Syntax: [..]

Description

Square brackets are used to denote arrays. To declare an array of 10 Int_t integers:-
Int_t my_ints[10];
and then to access the ith:-
my_ints[i]
Caution: C++ array indeces start from 0!!, so to sum all these integers:-
Int_t sum = 0;
for (Int_t index=0; index<10; index++) sum += my_ints[index];

C++ arrays are no more dynamic than FORTRAN ones, so if the application calls for arrays where the size won't be known until execution time then a container object i.e. one that can hold other objects should be used instead, such as ROOT's TObjArray.

Multi-dimensional arrays are possible, although the syntax can drive a FORTRAN programmer a little crazy: each dimension is in a separate pair of [] and the order is reversed, so:-

Int_t my_ints[5][10];
represents 5 arrays, each being an array of 10 integers. It may help to understand C++ nests constructs to develope new ones. So, rather than have multiply dimensioned arrays, C++ reuses the single array concept but allows arrays to be stored in arrays, hence an array of arrays. Then the [] closest to the identifier has to be the one that operates first, hence the ordering.

Multi-dimensional arrays are generally very inflexible, C++ won't allow you to play the same games changing array dimensionality when passing the array to a function. This makes writing general purpose matrix processing libraries difficult. Fortunately, C++ also provides a better way of think about matrices - as objects. In this way operations on matrices become member functions of a matrix class.

See operator precedence

Other Uses for [..]

None.

Usage Notes

None.
Go Back to the The C++ Crib Top Page


If you have any comments about this page please send them to Nick West