LectENG1008 6 Arrays
LectENG1008 6 Arrays
Arrays
Arrays may contain other data types, for e.g., float d[100]
Like any other variables, uninitialized array elements contain
garbage values
Note : Character strings and their similarity to arrays
To refer to a particular location or any one of these elements,
we specify or give the array’s name followed by the position
number of that particular element in square brackets ([ ]).
For example, c[3] where c is the array name and 3 is the
position number
The position number within square brackets is called a
subscript or index
The first element in every array is the zeroth element
The first printf statement (line 16) displays the column heads for the two
columns printed in the subsequent for statement
\=
+//
Passing Arrays to Functions
Passing Arrays to Functions
For example, the following statement sets all the elements in row 2 of
array a in Fig. 6.20 to zero:
for (column = 0; column <= 3; ++column) {
a[2][column] = 0;
}
We specified row 2, so the first subscript is always 2
The loop varies only the second subscript
The preceding for statement is equivalent to the assignment
statements:
a[2][0] = 0;
a[2][1] = 0;
a[2][2] = 0;
a[2][3] = 0;
Multidimensional Arrays
The following nested for statement determines the total of all the
elements in array a
total = 0;
for (row = 0; row <= 2; ++row) {
for (column = 0; column <= 3; ++column) {
total += a[ row ][ column ];
}
}
The for statement totals the elements of the array one row at a time
The outer for statement begins by setting row (i.e., the row subscript)
to 0 so that the elements of that row may be totaled by the inner for
statement
The outer for statement then increments row to 1, so the elements of
that row can be totaled and the same for row = 2
When the nested for statement terminates, total contains the sum of
all the elements in the array a
Multidimensional Arrays
A multidimensional array can be initialized when it’s defined, much like a
single-subscripted array
For example, a double-subscripted array int b[2][2] could be defined and
initialized with
int b[2][2] = {{ 1, 2 },{ 3, 4 }};
The values are grouped by row in braces
The values in the first set of braces initialize row 0 and the values in
the second set of braces initialize row 1
So, the values 1 and 2 initialize elements b[0][0] and b[0][1],
respectively, and the values 3 and 4 initialize elements b[1][0] and
b[1][1], respectively
If there are not enough initializers for a given row, the remaining
elements of that row are initialized to 0
Thus, int b[2][2] = {{ 1 }, { 3, 4 }};
would initialize b[0][0] to 1, b[0][1] to 0, b[1][0] to 3 and b[1][1] to 4
Bounds checking for Array Subscripts
It’s important to ensure that every subscript you use to access an array
element is within the array’s bounds - that is, greater than or equal to 0
and less than the number of array elements
A two-dimensional array’s row and column subscripts must be greater
than or equal to 0 and less than the numbers of rows and columns,
respectively
Allowing programs to read from or write to array elements outside the
bounds of arrays are common security flaws
Reading from out-of-bounds array elements can cause a program to
crash or even appear to execute correctly while using bad data
Writing to an out-of-bounds element (known as a buffer overflow) can
corrupt a program’s data in memory, crash a program and allow attackers
to exploit the system and execute their own code
As we stated earlier, C provides no automatic bounds checking for
arrays, so you must provide your own
Passing multidimensional arrays to
functions
Multidimensional Arrays
The program defines three arrays of two rows and three columns
(six elements each)
The definition of array1 (line 11) provides six initializers in two
sublists
• The first sublist initializes row 0 of the array to the values 1, 2
and 3; and the second sublist initializes row 1 of the array to
the values 4, 5 and 6
• If the braces around each sublist are removed from the array1
initializer list, the compiler initializes the elements of the first row
followed by the elements of the second row
The definition of array2 (line 12) provides five initializers
• The initializers are assigned to the first row, then the second row
• Any elements that do not have an explicit initializer are initialized
to zero automatically, array2[1][2] is initialized to 0
Passing Multidimensional arrays to
functions
The definition of array3 (line 13) provides three initializers in two
sublists
• The sublist for the first row explicitly initializes the first two elements
of the first row to 1 and 2
• The third element is automatically initialized to zero
• The sublist for the second row explicitly initializes the first element
to 4
• The last two elements are automatically initialized to zero
The program calls printArray (lines 26–41) to output each array’s
elements
The function definition specifies the array parameter as int a[][3].
• This is how double-subscripted or two-dimensional arrays are
specified as array parameters and passed to the function
• Recall for the single-dimensional array, the array parameter is just
int b[ ]
Passing Multidimensional arrays to
functions
When we receive a single-subscripted array as a parameter, the
array brackets are empty in the function’s parameter list
The first subscript of a multidimensional array is not required
either, but all subsequent subscripts are required
The compiler uses these subscripts to determine the locations in
memory of elements in multidimensional arrays
All array elements are stored consecutively in memory regardless
of the number of subscripts
In a double-subscripted array, the first row is stored in memory
followed by the second row
Providing the subscript values in a parameter declaration enables the
compiler to tell the function how to locate an element in the array
Character Arrays
Character Arrays
Sorting Arrays
Figure 6.15 sorts the values in the elements of the 10-element array
a (line 10) into ascending order
The technique we use is called the bubble sort or the sinking sort
because the smaller values gradually “bubble” their way upward to
the top of the array like air bubbles rising in water, while the larger
values sink to the bottom of the array
The technique is to make several passes through the array
Sorting Arrays