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

  1. /* ArrayTrickery.c - corrected*/
  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 SHOULD WORK on the MAC.
  8.  I have gotten it to work on a DEC/VAX also.
  9. */
  10.  
  11. /*  As a final note, I would encourage anybody programming
  12.     seriously in C to buy a copy of the text "Numerical Recipes in C".
  13.     This book provides a wealth of utility functions, numerical and otherwise.
  14.     Best of all, THEY WORK!
  15. */
  16.  
  17. /* I replaced the <= with < to avoid overstepping
  18.     array bounds. Sorry about that! */
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h> /* Oops! I forgot this before. */
  22.  
  23. main()
  24. {
  25.     float    ***array;
  26.     short    x=3, y=4, z=5, i, j, k;
  27.     
  28.     array = (float***)malloc(x*sizeof(float**));
  29.     for(i=0; i<x; i++)
  30.     {
  31.         array[i] = (float**)malloc(y*sizeof(float*));
  32.         for(j=0; j<y; j++)
  33.         {
  34.             array[i][j] = (float*)malloc(z*sizeof(float));
  35.             for(k=0; k<z;  k++)
  36.                 array[i][j][k] = i+j+k;
  37.         }
  38.     }
  39.     for(i=0; i<x; i++)
  40.     {
  41.         for(j=0; j<y; j++)
  42.         {
  43.             for(k=0; k<z;  k++)
  44.                 printf("\narray[%d][%d][%d] = %f",i,j,k,array[i][j][k]);
  45.         }
  46.     }
  47.     return 0;
  48. }
  49.