home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_07 / 9n07062a < prev    next >
Text File  |  1991-05-07  |  3KB  |  122 lines

  1.  
  2. /***********************************************************
  3. *                                                          *
  4. *       Listing #1                                         *
  5. *                                                          *
  6. *       Simulation of multi-dimensional arrays in          *
  7. *       Small C V2.0 using only 1-D arrays.                *
  8. *       This Small-C version is from CUG disk #163,        *
  9. *       and PC Club of Toronto #152.                       *
  10. *                                                          *
  11. *                                      -  Don Lang  1/91   *
  12. ***********************************************************/
  13.  
  14. /*
  15.   Declaration required for c.lib
  16.                 */
  17. extern int printf();
  18.  
  19. /* simulate: char garray[2][2] = { 1, 2, 3, 4 }; */
  20.  
  21. char garray[4] = { 1, 2, 3, 4 };
  22. int d_sizes[2] = { 2, 2 };
  23. int a_indices[2];
  24.  
  25. main()
  26. {
  27.     ga_print(2,2, garray);
  28. }
  29. /*
  30.      This global print function will print the contents
  31.      of a simulated 2-D integer array.
  32.                                */
  33. ga_print (a,b, array) int a,b; 
  34. char array[];
  35. {
  36.        int i,k;
  37.        printf("\n");
  38.        i=0;
  39.        while (i < a) {
  40.          for(k=0; k<b; k++) {
  41.           a_indices[0]=i;
  42.           a_indices[1]=k; /* array[i][k] */
  43.           printf(" %d ",
  44.           array[get_offset(2, d_sizes, a_indices)]);
  45.          }
  46.          printf("\n");
  47.          i++;
  48.        }
  49. }
  50. /*
  51.  
  52.    Function: get_offset
  53.  
  54.    This Small-C function can be used to simulate
  55.    multi-dimensional arrays by using an implicitly supplied
  56.    1 dimensional array.  The function simply returns the
  57.    offset into a declared 1 dimensional array that is used
  58.    as an index for the accessed element.  This function
  59.    will be moved into the modified compiler.
  60.  
  61.    Input parameters:
  62.  
  63.    num_dims:  The number of dimensions of the simulated
  64.           multi-dimensional array. (To be stored in
  65.           the compiler's local/global symbol tables.)
  66.  
  67.    d_sizes:   A pointer to a 1-D array that contains the
  68.           dimension sizes of the simulated array.  These
  69.           would normally appear within the non-simulated
  70.           array's declaration.  (To be stored in the
  71.           compiler's local/global symbol tables.)
  72.  
  73.    a_indices: A pointer to a 1-D array that is used to store
  74.           the indices into the n-dimensional simulated
  75.           array that would normally appear within the
  76.           non-simulated n-dimensional array reference.
  77.           (To be extracted by the compiler's expression
  78.           analyzer.)
  79.  
  80.     e.g.      To simulate the declaration:
  81.  
  82.           char target[3][4];
  83.  
  84.           We would in Small-C declare:
  85.  
  86.           char target[12];
  87.           int d_sizes[2] = { 3,4 };
  88.           int a_indices[2];
  89.  
  90.           And wanting "target[1][2];" assign
  91.  
  92.           a_indices[0] = 1;
  93.           a_indices[1] = 2;
  94.  
  95.           Then use:
  96.  
  97.           "target[get_offset(2, d_sizes, a_indices)]"
  98.  
  99.           in it's place.
  100.                         */
  101. get_offset (num_dims, d_sizes, a_indices)
  102. int num_dims, d_sizes[], a_indices[]; 
  103. {
  104.  
  105.     int adr_offset, dim_eval, dim_cnt, par_prod;
  106.     adr_offset = 0;
  107.     d_sizes[0] = 1;
  108. /*
  109.     Implement the arithmetic series
  110.                           */
  111.     for (dim_eval=0; dim_eval < num_dims; dim_eval++) {
  112.           par_prod = 1;
  113.           for (dim_cnt=num_dims-dim_eval-1; dim_cnt>=0;
  114.                           dim_cnt--)
  115.               par_prod *= d_sizes[dim_cnt];
  116.           adr_offset += a_indices[dim_eval] * par_prod;
  117.     }
  118.     return adr_offset;
  119. }
  120.  
  121.  
  122.