Array

Standard Input/Output
  • Need for an array

    • Many applications require the processing of multiple data items with
      • Common characteristics (e.g., same data type)
      • E.g., set of numbers, set of characters, set of names
  • Array

    • It is used to store
      • similar data items
      • in contiguous memory locations
      • under a single name
    • It holds a fixed number of equally sized data elements (of the same data type)
    • The individual elements are accessed by
      • specifying the subscript/index using [] operator
      • Index start from 0, increased by 1 for every element
    • Address of an element is calculated as
      • Name of an array refers to the base address of the array
      • Address of ith location = base address + (size of an element * index i)
    • E.g., an array of 3 integers
      • Size of each element is 4
      • Starting memory location is 1000
      • Address of 0th element = 1000 + 4*0 = 1000
      • Address of 1th element = 1000 + 4*1 = 1004
      • Address of 2nd element = 1000 + 4*2 = 1008
      • Total of 12 bytes are allocated in the memory of this array
  • Array Declaration

    • Arrays are declared with appropriate data type and size
    • Arrays can be of single dimension or multi dimensions
    • Array declaration reserves space in memory
    • General form:
      datatype arrayname[size];
  • Accessing Array Elements

    • The array elements are accessed by specifying the subscript/index inside [] operator
    • General form
    • General form:
      arrayname[index of the element]
    • Examples:
      x[0] -> access the 1st element in x
      x[3] -> access the 4th element in x
      str[2] -> access the 3rd character in string (str)
  • Array Initialization

    • Array elements can be initialized during declaration or in the program
    • When arrays are initialized during declaration, partial initialization is allowed
    • In partial initialization, the uninitialized elements are initialized to
      • Zero for numeric array
      • Null for character array
    • If initialized, array can be declared without specifying exact size
      • The size equals to the number of elements initialized
    • General forms:
      datatype arrayname[size] = {values(s)}
      datatype arrayname[] = {values(s)}
  • Basic Operation on Arrays

    • Basic operations allowed on arrays are
      • Storing array elements
      • Retrieving array elements
      • Processing array elements
      • Insertion and Deletion can be done by moving array elements to the appropriate places
    • Note that C doesn’t allow to assign an array to another array
      • E.g., arrayA = arrayB; is invalid
  • Advantages/Limitations of arrays

    • Advantages
      • Simple and easy to use
      • Stored in contiguous locations
      • Fast retrieval because of its indexed nature
      • No need to worry about the allocation and de-allocation of arrays
    • Limitations
      • Arrays are static in nature (fixed size). Memory is allocated at the beginning of the execution. If n is defined and used m -> wasted n-m
      • No automatic array bounds checking during compilation