TYPE array_var_name[SIZE]; // Uninitialized |
int A[10];
// A is an array of 10 integers
// All elements are uninitialized
int B[10] = {11, 12, 13, 14, 15 };
// B is an array of 10 integers
// First 5 elements are initialized
int C[] = {21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
// C is an array of 10 integers
// All 10 elements are initialized
|
So the indices of an array of size N are:
int i;
for (i = 0; i < N ; i = i + 1)
{
A[i]...
}
|
 
For example, a two dimensional array can be imagined as a Two dimensional table of a certain data type:
|
jimmy is an two dimensional array of 3x5 values of type int.
The way to define this array is:
int jimmy [3][5]; |
|
int A[10][5][20]; |
|
|
|
|
This is possible because arrays (in C/C++) are stored in row major manner.
|
Then we can find the location of any element of that array.
int A[10][10]; | | +----------+ A: | | <--- A = address of first element +----------+ | | +----------+ | | +----------+ | | +----------+ A[i][j] | | <--- address = A + (i*10 + j) +----------+ | | +----------+ | | +----------+ | | +----------+ | | |
#define MAX 5
int A[MAX];
void Change(int A[])
{
int i;
for (i = 0; i < MAX; i = i + 1)
{
A[i] = A[i] + 1000;
}
}
int main ()
{
Change(A);
}
|
NOTE:
| The effect of pass-by-reference is clearly seen by the change of the values in the array elements |
int jimmy[3][5];
jimmy[i][j] = 1234;
|
#define WIDTH 5
#define HEIGHT 3
int jimmy [HEIGHT][WIDTH];
void Print(int A[])
{
int i,j;
for (i = 0; i < HEIGHT; i = i + 1)
{
for (j = 0; j < WIDTH; j = j + 1)
cout << A[i*WIDTH + j] << "\t";
cout << "\n";
}
}
int main ()
{
Print( jimmy );
}
|
Error: Formal argument A of type int* in call to Print(int*) is being passed int(*)[5]. |
|
What is the type
of the variable
A
if
A
is a one-dimensional array:
|
To answer this question, we look at how the array is stored
int A[10]; | | +---------+ | | +---------+ A: | A[0] | (4 bytes) +---------+ | A[1] | (4 bytes) +---------+ | A[2] | (4 bytes) +---------+ | ... | +---------+ | | |
Observation:
|
The value
A
is an
address of an integer variable
I.e.: the type is (int *) |
(int *) jimmy |
#define WIDTH 5
#define HEIGHT 3
int jimmy [HEIGHT][WIDTH];
void Print(int A[])
{
int i,j;
for (i = 0; i < HEIGHT; i = i + 1)
{
for (j = 0; j < WIDTH; j = j + 1)
cout << A[i*WIDTH + j] << "\t";
cout << "\n";
}
}
int main ()
{
Print( (int *) jimmy );
}
|
This is done by defining the array inside a struct construct
#define MAX 5
struct Array
{
int x[MAX];
} A;
void Change(struct Array A)
{
int i;
for (i = 0; i < MAX; i = i + 1)
{
A.x[i] = A.x[i] + 1;
}
}
int main ()
{
Change(A);
}
|
NOTE:
| The effect of pass-by-value is clearly seen by NO changes made to the values in the array elements |
NOTE: the struct construct will be covered in detail later....
The reason for all but one dimensions is clear by using an example:
int A[3][4]; | | +---------+ A: | A[0][0] | <---- Base +---------+ | A[0][1] | +---------+ | A[0][2] | +---------+ | A[0][3] | +---------+ | A[1][0] | <---- Base + 1*4 +---------+ | A[1][1] | +---------+ | A[1][2] | +---------+ | A[1][3] | +---------+ | A[2][0] | <---- Base + 2*4 +---------+ | | |
(The C/C++ compiler does NOT need to know the size of the first dimension)
int main(int argc, char *argv[])
{
void Print(float [][4]); // How to declare function
// with 2-dim ARRAY parameter
// NOTE: parameter and the
// first dimension is NOT
// required
// You MAY write:
// void Print(float X[3][4]);
float A[3][4];
int i, j, k;
k = 1;
for (i = 0; i < 3; i = i + 1)
for (j = 0; j < 4; j = j + 1)
{
A[i][j] = k;
k = k + 1;
}
Print(A); // Passing an ARRAY parameter
}
void Print(float H[][4]) // How to define function
// with array parameter
{
int i, j, k;
for (i = 0; i < 3; i = i + 1)
{
for (j = 0; j < 4; j = j + 1)
cout << H[i][j] << "\t";
cout << "\n";
}
}
|