Page 83 - PowerPoint Presentation
P. 83

CAVITE STATE UNIVERSITY
                               T3 CAMPUS
                               Department of Information Technology      DCIT 25 – Data Structures and Algorithms














                       The figure above declares a multidimensional array of integers. The first set of array
               elements  contains  three  array  elements,  one  for  each  student.  The  second  set  of  array
               elements has four array elements. The first of the four elements contain student ID and the
               other three contains the three grades for that student ID.
                       In addition to organizing data stored in elements of an array, a multidimensional array
               can store memory addresses of data in a pointer array and an array of pointers to pointers.

               Multidimensional Array in Memory
                       Data stored in multidimensional array is stored sequentially by sets of elements, as
               shown in the next figure. The first set of four array elements is placed in memory, followed by
               the second set of four array elements, and so on.
                       The  name  of  a  multidimensional  array  references  the  memory  address  of  the first
               element of the first set of four elements. That is, grades is the equivalent of using memory
               address 1 in the figure. You can use the name of a multidimensional array as a pointer to the
               entire array.
                       The index of the first element of the first set of array elements points to the memory
               address where values assigned to array elements are stored.






                       Referencing the index of the first dimension points to the memory address of the first
               element of that dimension. For example, referencing grades[1] points to memory address 9
               in in figure. Memory address 9 is the first memory address of contiguous memory where values
               of the set of array elements that are associated with grades[1] are stored.

               Declaring a Multidimensional Array
                       A multidimensional array is declared similar to the way you declare a one-dimensional
               array except you specify the number of elements in both dimensions.
                                                    int grades[3][4];

                       The first bracket [3] tells the compiler that you’re declaring 3 pointers, each pointing to
               an  array.  This  concept  might  be  confusing  because  the  term  “pointer”  may  make  some
               programmers think of pointer variable or pointer array. However, we are not talking about a
               pointer  variable  or  pointer  array.  Instead,  we  are  saying  that  each  element  of  the  first
               dimension of multidimensional array reference a corresponding second dimension, which is
               an array.
                       In this example, all the arrays pointed to by the first index are of the same size. The
               second index can be of variable size. For example, the previous statement declares a two-




                                                                                                 Page | 30
   78   79   80   81   82   83   84   85   86   87   88