Basic Concepts of Arrays

This article introduces the following topics:

  1. Introduction to arrays with examples
  2. Types of arrays with examples
  3. Initialization of arrays
  4. Example with C program

Introduction to Arrays

An array can be defined as an area in memory. This can be referred to by a common name. Arrays are used to set a group like objects.

Each element of the array can be referred to by the array name and a subscript or index.

Syntax:

  1. Datatype arrayname [n];  
Types of Array with example

There are the following two types of arrays: 
  • One-dimensional / Single –dimensional array
  • Two-dimensional / Multi-dimensional arrays

One-dimensional / Single –dimensional array

An array is declared as containing a certain datatype and internally they are stored as a contiguous set of these data types.

  1. Int numbers [10];  
This defines an integer array called numbers. The array can hold 10 integer values that will store the elements numbered 0 to 9.
  1. Float fnums [10];  
This defines a float array called fnums. The array can hold 10 float values, that will be stored the elements fnums 0 to fnums 9.
  1. Char name [10];  
This defines a character array called name. The array can hold 10 Char values that will store the elements name 0 to name 9.

Initialization of One-dimensional/Single-dimensional Array

An array can be initialized, when declared by specifying the vales of some or all of its elements. This initialization is at the time of declaration is possible only outside a function unless it is declared static.

Initialization can also be done inside the function after the array has been declared by accepting the values.
  1. Char name [7] = {‘K’,’r’,’i’,’s’,’h’,’n’,’a’, \0’};  
  2. Char name [7] = {‘Krishna’};  
In that first case individual elements have been moved into the array and hence it is essential that the name terminator be specified explicitly.

In that second case, the name terminator gets attached automatically because a name has been assigned.
  1. Int ages [] = {19, 23, 45.27, 28, 29};  
In that case an integer and float individual value must be moved into the elements. In the proceeding declaration the dimension has not been specified for the reason of flexibility, the compiler will automatically calculate the dimension based on the initialization.

Two-dimensional / Multi-dimensional arrays

A two-dimensional array is in essence an array of one–dimensional arrays. It is also known as a “two-d array”.

Syntax:
  1. Datatype arrayname[x] [y];  
Each dimension of the two-d array has been placed in a separate set of brackets. Two-d arrays are stored in a row–column matrix, in which the first index indicates the row and the second indicates the column. To access a specific element, either the indices or subscripts need to be specified.

Initializing Multi-dimensional/ Two-dimensional arrays

The rules for initialization of a two-d array are the same as a one-dimensional array. Initialization at the time of declaration must be done outside the function or must be declared static within the function.

For example:
  1. int squares [10] [2] =   
  2. {  
  3. {1, 1},  
  4. {1, 2},  
  5. {2, 3},  
  6. {3, 4},  
  7. {4, 5},  
  8. {5, 6},  
  9. {6, 7},  
  10. {7, 8},  
  11. {8, 9},  
  12. {9, 0}  
  13. };  
For example:
  1. Int sales [3] [4] = {  
  2. {143, 234},  
  3. {253, 567,546},  
  4. {786, 657, 890, 543}  
  5. };  
Only the two elements of the first row, three in the second row and four in the third row are initialized.

As usual, the array can also be initialized inside the function by accepting the values.

Character arrays are initialized as two-dimensional arrays:
  1. Char name [7] [3] = {  
  2. {‘Krishna’},  
  3. {‘Radikaa’},  
  4. {‘Govinda’}  
  5. };  

Up Next
    Ebook Download
    View all
    Learn
    View all