Friday, January 13, 2012

Array Declaration


ARRAY DECLARATION
Declaring the name and type of an array and setting the number of elements in the array is known as dimensioning the array.
    The array must be declared before one uses it in a C++ program like other variables. In the array declaration, one must define the type of the array, name of the array, number of subscripts in the array and the total number of memory locations to be allocated.
Before any liner or multidimensional array is used in a program, one must provide to the compiler or interpreter the following information.
  1. Type of the array (i.e., integer, floating point, char type etc.)
  2. Name of the array
  3. Number of subscripts in the array (i.e., whether the array is one dimensional or two dimensional etc.)
  4. Total number of memory locations to be allocated or more specifically, the maximum value of each subscript)
In general, one dimensional array may be expressed as
                storage_class   data_type  array_name  [expression]
  where the storage_class refers to the scope of the array variables such as external, static, or an automatic; and data_type is used to declare the nature of the data elements stored in the array, like character type, integer and floating point. Array_name is the name of the array, and an expression is used to declare the size of the memory locations required for further processing by the program.
   The storage_class is optional. Default values are automatic for arrays defined within a function or a block and extended for arrays defined outside the function.
   Some valid one dimensional array declarations are:
        int marks [300];
      char line [90];
      static char page [8];
      float coordinate [400];
  In the above illustrations, the first one has been declared as a mark of 300 integer numbers; the second is a line of 90 characters; the third is a static array which consists of 8 characters and the fourth is a set of 400 floating point numbers stored in the array of coordinate.
     Some invalid array declaration are:
          int value [0];
        static int values [0.002];
        float numbers [-90];
        char s[$];
  For an expression, positive integer numbers should be placed for the memory allocations whenever an array in C++ is declared.

No comments:

Post a Comment