Book
Collection
Click for Table of Contents
© 2025 by Rance D. Necaise
C Primer for Python Programmers
Copyright © 2025
Rance D. Necaise

7.4 Accessing Array Elements

The entire contents of an array are identified by a single name. The values stored in an array are called its elements. Each element has a position number specified as an integer, called an index. An individual element within the array can be accessed directly by specifying its index or subscript, which indicates an offset from the start of the array. As in Python, this is done with the use of square brackets []. For example, consider the values array created in the previous section

The elements are numbered in sequential order starting at 0. Thus, to access the first element, we would use the notation

  values[0]

or to access the fifth element, which is identified by index value 4, we would use the notation

  values[4]

Setting An Element

Each element of an array is treated like a variable of the given type. For example, the values array has 5 elements, in which each element can store an integer value. Thus, each individual element is treated just like a regular integer variable. That is, the element can be used anywhere an integer variable can be used.

To set the value of an array element, we simply assign the value to the specific element, which is indicated using the subscript notation

  1.   values[3] = 12;

Here, we set or change the value of element 3 (the element with index value 3) to 12

The array subscript can be specified by an integer variable, which is useful if we need to compute the subscript during the execution of a program

  1.   int i = 0;
  2.   values[i] = 25;
Question 7.4.1

What values does the value array contain after the following statements are executed?

  1.  values[i+2] = 38;
  2.  values[4] = 0;

Getting an Element

To obtain the value of an array element, you use the subscript notation to indicate the specific element. Consider the statement below which prints the value in element 4 of the data array:

  1.   printf("The result = %d\n", data[4]);

which results in the following output:

The result = 15

As indicated earlier, an individual element can be used anywhere a variable of the same data type can be used. Here, we assign the value of element 3 to the integer variable x and print its value to the terminal

  1.   int x;
  2.   x = data[3];
  3.   printf("The result = %d\n", x);

which results in the output

The result = 8

Suppose we want to print the entire contents of an array. We can access the individual elements within a loop and use the loop variable to indicate the index of the individual elements:

  1. int i = 0;
  2. while(i < 5) {
  3.   printf("%d\n", values[i]);
  4.   i++;
  5. }

Note that unlike in Python, there is no function that can be used to obtain the size of an array. Further, there is no length information maintained for arrays at all. Thus, when iterating over an array, you must know the number of elements in the array.

Note
Note

The sizeof function should not be used to try and determine the size of an array. You will find many examples of it being used in this fashion, but that only works when used in the same function in which the array was created. Thus, it is best practice to never use the sizeof function with arrays.

Code Tracer

Consider the code segment below which creates an array and then initializes it using a loop. Trace through the code to see how the individual elements are accessed.

Question 7.4.2

Consider the following code segment

  1. const int INPUT_SIZE = 10;
  2. int inputs[INPUT_SIZE];
  3.  
  4. for(int i = 0; i < INPUT_SIZE; i++)
  5.   inputs[i] = i * 5 + 100;

What values are contained in the inputs array after the code segment is executed?