Programming in C: Unit II (a): Arrays

Accessing the Elements of an Array in C

with Examples

For accessing an individual element of the array, the array subscript must be used. For example, to access the fourth element of the array, we must write arr [3].

ACCESSING THE ELEMENTS OF AN ARRAY

For accessing an individual element of the array, the array subscript must be used. For example, to access the fourth element of the array, we must write arr [3]. The subscript/ index must be an integral value or an expression that evaluates to an integral value.

Programming Tip: To access all the elements of the array, you must use a loop. There is no single statement that can do the work.

Although storing the related data items in a single array enables the the programmers develop concise and efficient programs, there is no single function that can operate on all the elements of the array. To access all the elements of the array, we must use a loop. That is, we can access all the elements of the array by varying the value of the subscript into the array. But note that the subscript must be an integral value or an expression that evaluates to an integral value. As shown in Figure 5.2, the first element of the array marks [10] can be accessed by writing, marks [0]. Now to process all the elements of the array, we will use a loop as shown in Figure 5.3.

The code accesses every individual element of the array and sets its value to -1. In the for loop, first the value of marks [0] is set to -1, then the value of the index (i) is incremented and the next value, i.e., marks [1] is set to -1. The procedure is continued until all the 10 elements of the array are set to -1.

Note

There is no single statement that can read, access, or print all the elements of the array. To do this, we have to do it using a for/while/do-while loop to execute the same statement with different index values.

Calculating the Address of Array Elements

You must be wondering that how C knows where an a individual element of the array is located in the memory.

The answer is that the array name is a symbolic reference to the address of the first byte of the array. When we use the array name, we are actually referring to the first byte of the array.

The subscript or the index represents the offset from the beginning of the array to the element being referenced. With just the array name and the index, C can calculate the address of any element in the array.

Since an array stores all its data elements in consecutive memory locations, storing just the base address, i.e, the address of the first element in the array is sufficient. The address of other data elements can simply be calculated using the base address. The formula for doing this calculation is:

Address of data element, A[k] = BA(A) + w (k-lower bound)

Here, A is the array, k is the index of the element for which we have to calculate the address, BA is the base address of the array A, w is the word size of one element in memory (for example, size of int is 2), and lower_bound is the index of the first element in the array.

Example 5.1

Given an array int marks[ ] = {99, 67, 78, 56, 88, 90, 34, 85}. Calculate the address of marks[4] if base address = 1000.

Solution

We know that storing an integer value requires 2 bytes, therefore, word size is 2 bytes.

Address (Marks [4]) = 1000+ 2(4 – 0)

= 1000 + 2 (4) = 1008

Example 5.2

Given an array, float avg[ ] = (99.0, 67.0, 78.0, 56.0, 88.0,90.0, 34.0, 85.0). Calculate the address of avg [4] if base address = 1000.

Solution

We know that storing a floating point number requires 4 bytes, therefore, word size is 4 bytes.

Address (Avg [4]) = 1000+ 4 (4 – 0)

= 1000+ 4 (4)

= 1016

Note

When we write arr[i], the compiler interprets it as the contents of memory slot which is i slots away from the beginning of the array arr.

Calculating the Length of an Array

Length of the array is given by the number of elements stored in it. The general formula to calculate the length of the array is:

Length = upper_bound - lower bound + 1

where upper bound is the index of the last element and lower_bound is the index of the first element in the array.

Usually, lower_bound is zero but this is not a compulsion as we can have an array whose index may start from any non-zero value.

Example 5.3

Let Age [5] be an array of integers such that

Age [0] = 2 Age [1] = 5 Age [2] = 3

Age [3] = 1 Age [4] = 7

Solution

Show the memory representation of the array and calculate its length.

Memory representation of the array Age is as given

Ppppppppppppppppp

Length = upper_bound lower_bound + 1

Here, lower bound 0, upper_bound = 4

Therefore, length = 4 - 0 + 1 = 5

Programming in C: Unit II (a): Arrays : Tag: : with Examples - Accessing the Elements of an Array in C