3. Introduction
• An array is a sequence of objects all of which have the
same type.
• The objects are called the elements of the array and are
numbered consecutively 0, 1, 2, 3, ... . These numbers are
called index values or subscripts of the array.
3
5. • Printing a Reverse in Order:
PROCESSING ARRAYS
5
6. • Initializing an Array
• The first line declares a to be the array of 3 elements. The second line
uses the sizeof() function to compute the actual number of elements in
the array.
• The value of sizeof(float) is 4 because on this machine a float value
occupies 4 bytes in memory. The value of sizeof(a) is 12 because the
complete array occupies 12 bytes in memory. Therefore, the value of
size is computed to be 12/4 = 3.
6
7. float a[7] = { 55.5,66.6, 77.7 };
• This array is declared to have 7 elements of type float;
then its initializer list initializes the first 3 elements with
the given values and the remaining 4 elements with the
value 0.
PROCESSING ARRAYS
7
9. • Allowing an Array Index to Exceed its Bounds
The last three values printed are garbage values.
9
10. • Passing an Array to a Function that Returns its Sum
10
11. • Explanation:
• When an array is passed to a function, as in the call
sum(a,size) in previous example, the value of array name
a is actually the memory address of the first element
(a[0]) in the array.
• The function uses that address value to access and
possibly modify the contents of the array. So passing an
array to a function is similar to passing a variable by
reference: the function can change the values of the
array’s elements.
11
12. • Printing the Memory Location of an Array
• This program prints the value of the address stored in an
array name.
• Its value is the memory address of the first byte of the
first element a[0] in the array.
12