It is also possible to represent multidimensional array in terms of a pointer as with the case of one dimensional array.A two-dimensional array is actually a collection of one-dimensional array.Therefore, a two-dimensional array can be defined as a pointer to a group of contiguous one dimensional arrays.Thus, a two-dimensional array declaration can be written as
data-type (*ptvar)[expression 2];
rather than
data-type array[expression 1][expression 2];
This concept can be generalized to higher dimensional arrays; that is,
data-type (*ptvar)[expression 2][expression 3].........[expression n];
replaces
data-type array[expression 1][expression 2]...........[expression n];
In the declaration data-type refers to the data type of the array, ptvar is the name of the pointer variable, array is the corresponding array name, and expression 1, expression 2,...........,expression n are positive-valued integer expression that indicate the maximum number of array elements associated with each subscript.
Suppose x is a two-dimensional integer array having 10 rows and 20 columns.We can declare x as
int (*x)[20];
rather than
int x[10][20];
In the first declaration, x is defined to be a pointer to a group of contiguous, one-dimensional, 20-element integer arrays.Thus, x points to the first 20-element array, which is actually the first row (i.e. row 0) of the original two-dimensional array. Similarly, (x+1)point to second 20-element array, which is the second row (row 1) of the two-dimensional array, and so on.
Now consider a three-dimensional floating point array t.This array can be defined as
float (*t)[20][30];
rather than
float[10][20][30];
Thursday, December 31, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment