C : ARRAYS


ARRAYS



DEFINITION AND DECLARATION OF AN ARRAY :

  1. An ARRAY is a collection of a similar elements i.e collection of variables of same data-types.

  2. To declare an ARRAY in we must specify the type of the elements and the number of elements required by an array.

  3. SYNTAX : data_type array_name[size];

  4. The size must be an integer constant greater than zero and data_type can be any valid C datatype.

  5. This types of ARRAY are known as SINGLE-DIMENSION ARRAY.

  6. A specific element in an ARRAY is accessed by an index.


PROCESSING OF ARRAYS :

  1. If a and b are similar arrays (i.e., same data type, same dimensionality and same size), assignment operations, comparison operations, etc. must be carried out on an element-by-element basis.

  2. This is usually accomplished within a loop, where each pass through the loop is used to process one array element.

  3. The number of passes through the loop will therefore equal the number of array elements to be processed.


PASSING ARRAYS TO FUNCTIONS :

  1. We can pass entire array to a function as an argument.

  2. To pass an array to a function, the array name must appear by itself, without brackets or subscripts, actual argument within the function call.

  3. When declaring a one- dimensional array as a formal argument, the array name is written with a pair of empty square brackets.

  4. The size of the array is not specified within the formal argument declaration.

  5. An empty pair of square brackets must follow the name of each array argument, thus indicating that the argument is an array.

  6. If argument names are not included in a function declaration, then an empty pair of square brackets must follow the array argument data type.


MULTI-DIMENTIONAL ARRAYS :

  1. Multidimensional arrays are defined similar to one-dimensional arrays, except that a separate pair of square brackets is required for each subscript.

  2. Thus,a two-dimensional array will require two pairs of square brackets, a three-dimensional array will require three pairs of square brackets, and so on.

  3. Data in MULTI-DIMENTIONAL ARRAY is stored in tabular format.

  4. SYNTAX : data_type array_name[type 1][type 2]...[type n];

  5. INITILIZING A MULTI-DIMENTINAL ARRAY :
        
        int array1[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
        
        

    OR
        
        int array1[3][4]={ {1,2,3,4},{5,6,7,8},{9,10,11,12} };
        
        


ARRAYS AND STRINGS :

  1. strcmp() :

    1. The strcmp() function accepts two strings as arguments and returns an integer value, depending upon the relative order of the two strings, as follows:
      1. A negative value if the first string precedes the second string alphabetically.

      2. A value of zero if the first stringand the second string are identical (disregardingcase).

      3. A positive value if the second string precedesthe first string alphabetically.


    2. Therefore, if strcmp( stri ngl , string2) returns a positive value, it would indicate that string2 must be moved, placing it ahead of stringl in order to alphabetize the two strings properly.


  2. strcpy() :

    1. The strcpy() function also accepts two strings as arguments.

    2. If first argument is generally an identifier that represents a string.
      The second argument can be a string constant or an identifier representing another string.

    3. The function copies the value of string2to stringl. Hence, it effectively causesone string to be assigned to another.


Comments