6.3. Declaring and Accessing Arrays

Declaring an array is much the same as declaring any other variable except that you must specify the array size. The size (or number of elements) is an integer value placed in square brackets after the arrays identifier.

Example 6-1. first_arrays.c

int
main()
{
  int person[10];
  float hourly_wage[4] = {2, 4.9, 10, 123.456};
  int index;

  index = 4;
  person[index] = 56;

  printf("the %dth person is number %d and earns $%f an hour\n",
         (index + 1), person[index], hourly_wage[index]);

  return 0;
}
      

NOTE: it is up to you to make sure you don't try to access an element that is not in the array such as the eleventh element of a ten element array. Attempting to access a value past the end of an array will either crash your program or worse, it could retrieve garbage data without telling you that an error occurred.