Sunday, July 15, 2007

Arrays

* Array in C
- No way to process an array as a whole unit in C.
- Only one dimension array exists. And the size of the array must be known in compilation.
- Arrays could not be copied and compared with each other directly.
- Arrays could not be returned from functions.
- Two things could be done to an array: to get the size of the array and to get the pointer to the first element of this array. All other operation on arrays would be done with this pointer.
- Two operators with the array name would do with the whole array: & and sizeof. & would get the address of this array, i.e. it is supposed to assign one pointer to the array. The size of array in byte would be obtained with sizeof. Another situation is in printf, which prints the whole string with the name of the array/pointer(NO *) and `%s'. For all other cases, the array name means the CONST pointer to the first element of the array. But when the array name is used as a function argument, the const property is lost. Changes are allowed on it in this case.
- The Nth element of an array does not exist. But its address could be used to do some checking, like
    if (p != &array[N]) p++;
- Multi-dimensional array
Note in C only one dimensional array exists. Multi-dimensional array is of linear memory mapping. Another way to get multi-dimensional array is to use multi-dimensional pointer and dynamic allocation. Note in this way the memory mapping might not be linear unless the total size of multi-dimensional array is allocated in the beginning.

No comments: