Previous: 9.5 An Example: Linear Algebraic Equations
Up: 9.1 Two Dimensional Arrays
Next: 9.7 Summary
Previous Page: 9.5 An Example: Linear Algebraic Equations
Next Page: 9.7 Summary

9.6 Common Errors

  1. Failure to specify ranges of smaller dimensional arrays in declaration of formal parameters. All but the range of the first dimension must be given in a formal parameter declaration. Example:
    init2(int aray2[][])
         {
              ...
         }
    Error! aray2 is a pointer to a two dimensional array, i.e. it points to an object that is a one-dimensional array, aray2[0]. Without a knowledge of the size of the object, aray2[0], it is not possible to access aray2[1], aray2[2], etc. Consequently, one must specify the number of integer objects in aray2[0]:
    init2(int aray2[][COLS])
         { ...
         }
    Correct! aray2[0] has COLS objects. It is possible to advance the pointer, aray2 correctly to the next row, etc.

  2. Failure to pass arguments correctly in function calls:
    init2(aray2[MAX][COLS]);
         init2(aray2[][COLS]);
         init2(aray2[][]);
    All of the above are errors. A two dimensional array name is passed in a function call:
    init2(aray2);

  3. Confusion between pointers to different types of objects. For example, in the above, aray2 points to an array object, aray2[0], whereas aray2[0] points to an int object. The expression aray2 + 1 points to aray2[1], whereas aray2[0] + 1 points to aray2[0][1]. In the first case the pointer is increased by COLS integer objects, whereas in the second case the pointer is increased by one integer object.

  4. Confusion between arrays of character strings and arrays of character pointers:
    char table[MAX][SIZE], *ptraray[MAX];
    The first declares table to be a two dimensional array that can be used to store an array of strings, one each in table[0], table[1], table[i], etc. The second declares ptraray to be an array, each element of which is a char *. Read the declaration from the end: [MAX] says it is an array with MAX elements; ptraray is the name of the array; char * says each element of the array is a char *. Properly initialized with strings stored in table[][], table[i] can point to a string. Properly initialized with pointers to strings, ptraray[i] can also point to a string. However, table[MAX][SIZE] provides memory space for the strings, whereas ptraray[MAX] provides memory space only for pointers to strings. Both pointers may be used in a like manner:
    puts(table[i]);
         puts(ptraray[i]);
    They will both print the strings pointed to by the pointers.



Previous: 9.5 An Example: Linear Algebraic Equations
Up: 9.1 Two Dimensional Arrays
Next: 9.7 Summary
Previous Page: 9.5 An Example: Linear Algebraic Equations
Next Page: 9.7 Summary

tep@wiliki.eng.hawaii.edu
Wed Aug 17 09:20:12 HST 1994