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

  1.  
  2. /*******************************************************
  3. *                                                      *
  4. *   Listing #3                                         *
  5. *                                                      *
  6. *   Full test case for addition of multi-dimensional   *
  7. *   arrays to Small C V2.0 using the modified          *
  8. *   CUG disk #163, and PC Club of Toronto #152.        *
  9. *                                                      *
  10. *                                 -  Don Lang  1/91    *
  11. *******************************************************/
  12.  
  13. #define LASTROW   1
  14. #define CLOSELAST 2
  15. /*
  16.   Declaration required for c.lib
  17.                 */
  18. extern int printf();
  19. /*
  20.     Test for global declaration of a 2-D array and for
  21.     proper initialization.  This will exercise the modified
  22.     Small C functions, "declglb", "initials", and "addsym."
  23.     Size of 1st dimension is determined by the number of
  24.     initialized elements as per the C language definition.
  25.                               */
  26. char garray[][2] = { 1, 2, 3, 4 };
  27.  
  28. int garray2 [3] [3];
  29.  
  30. /* Test to see that 1-D is unaffected. */
  31.  
  32. char gar1D[] = { 1,2 };
  33.  
  34. /*
  35.    Try an external 2-D array of unknown size.
  36.                           */
  37.     /*   (in file: ext.c)    */
  38.  
  39. extern char ext2_D [] [CLOSELAST + 2];
  40.  
  41. main()
  42. {
  43.     /*
  44.        Declare local 2-D array and thus test the
  45.        function "declloc" for 2-D.
  46.                             */
  47.     int i, k, count, larray[3][4];
  48.     count = 0;
  49.     for (i = 0; i < 3; i++)
  50.            for (k=0; k < 4; k++) {
  51.               ext2_D[i][k] = larray[i][k] = count;
  52.               count++;
  53.            }
  54. /*
  55.        The following strange code has been written to test
  56.    the compiler's ability to correctly generate code for
  57.    multi-dimensional array expressions using both variable
  58.    and constant expressions as array indices.  This, and the
  59.    above nested "for" loop, will test the modifications to
  60.    the Small-C expression analyzer; specifically the
  61.    function "heir14."
  62.                               */
  63.     for (i=0; i<3; i++) {
  64.            printf("\n");
  65.            printf(" %d ", larray[i][0]);
  66.            printf(" %d ", larray[i][LASTROW]);
  67.            printf(" %d ", larray[i][2]);
  68.            printf(" %d ", larray[i][3]);
  69.     }
  70.     printf("\n"); 
  71.     printf("\n");
  72.     for (i=0; i<4; i++) printf(" %d ",
  73.               larray[CLOSELAST-2][i]);
  74.     printf("\n");
  75.     for (i=0; i<4; i++) printf(" %d ",
  76.               larray[CLOSELAST/2][i]);
  77.     printf("\n");
  78. /*
  79.     Try printing initialized global character array and the
  80.     processed external.
  81.                           */
  82.     ga_print(2,2, garray);
  83.     for (i=0; i<3; i+=1) {
  84.         printf("\n");
  85.         for (k=0; k<4; k++) printf(" %d ",
  86.                        ext2_D[i][k]);
  87.     }
  88. /*
  89.    Print out 1-D array to see that it hasn't been changed by
  90.    the modifications for "n" dimensions.
  91.                               */
  92.     printf("\n");
  93.     ga2_print(2, gar1D);
  94. }
  95. /*
  96.     This global print function will test passing of array
  97.     arguments, and thus the modified Small C function
  98.     "doargs."
  99.                               */
  100. ga_print (a,b, array) int a,b; char array[][2];
  101. {
  102.     int i,k;
  103.     printf("\n");
  104.     i=0;
  105.     while (i < a) {
  106.         for(k=0; k<b; k++) printf(" %d ",
  107.                        array[i][k]);
  108.         printf("\n");
  109.         i++;
  110.     }
  111. }
  112. ga2_print (a, array) int a; char array[];
  113. {
  114.     int i;
  115.     printf("\n");
  116.     for (i = 0; i < a; i++) printf(" %d ", array[i]);
  117. }
  118.  
  119.     ext.c
  120.  
  121. char ext2_D [3] [4]; /* Try an external 2-D array */
  122.  
  123.