Arrays in C/C++
Arrays declaration
int A[20]; // integer array with 20 elements,
// 0 <= index <= 19
int B[5][6]; // integer array with 5x6 elements
// array of arrays
typedef char String10[11];
String10 student[30];
Arrays access
A[4] = 37;
cout << A[4]; // prints 37
cout << A[4] + A[4]; // prints 74
B[3][2] = 66;
cout << B[3][2];
Arrays initialization
int A[5] = {3, 17, -9, 8 ,2};
int B[3][2] =
{
{3, 8},
{5, 8},
{17, -9}
}
Examples
- Initializing an array with a declaration
- Compute the sum of the elements of the array
A few Warnings
- No array aggregate operation. Don't do
A = B;
- No out-of-bounds index check.
- By default an array is passed by REFERENCE.
Passing Arrays to Functions
fig04_14.cpp
Multiple-Subscripted Arrays
fig04_23.cpp