home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / ArrayTrickery in C / MacArrayTrickery.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-03  |  1.1 KB  |  40 lines  |  [TEXT/KAHL]

  1. //• MacArrayTrickery.c - corrected THINK C version
  2. //• Dynamically create multidimensional arrays in C.
  3. //• From TrickyS@aol.com, aka meidscsinc@engvms.unl.edu
  4. //• Inspired by AFC Radix1@aol.com, MCart@aol.com, &RickGenter@aol.com,
  5. //• who all answered my inquiry as to how to do this!
  6. //• Updated for Squeegee.
  7. //• This version works on the MAC using THINK Cv6.
  8.  
  9. /*  As a final note, I would encourage anybody programming
  10.     seriously in C to buy a copy of the text "Numerical Recipes in C".
  11.     This book provides a wealth of utility functions, numerical and otherwise.
  12.     Best of all, THEY WORK!
  13. */
  14.  
  15. /* I replaced the <= with < to avoid overstepping
  16.     array bounds. Sorry about that! */
  17.  
  18. #include <Memory.h>
  19.  
  20. main()
  21. {
  22.     float    ***array;
  23.     short    x=3, y=4, z=5, i, j, k;
  24.     
  25.     array = (float***)NewPtr(x*sizeof(float**));
  26.     for(i=0; i<x; i++)
  27.     {
  28.         array[i] = (float**)NewPtr(y*sizeof(float*));
  29.         for(j=0; j<y; j++)
  30.         {
  31.             array[i][j] = (float*)NewPtr(z*sizeof(float));
  32.             for(k=0; k<z;  k++)
  33.                 array[i][j][k] = i+j+k;
  34.         }
  35.     }
  36.     //• No output produced, but it works. Run it with the Debugger on
  37.     //• to see for yourself that it is really working!
  38.     return 0;
  39. }
  40.