home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 19081 < prev    next >
Encoding:
Text File  |  1992-12-31  |  4.2 KB  |  127 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!think.com!ames!nsisrv!ame.gsfc.nasa.gov!almeida
  3. From: almeida@ame.gsfc.nasa.gov (aswin m. almeida)
  4. Subject:  allocs, multidim arrays, and FAQ
  5. Message-ID: <1992Dec31.170307.4524@nsisrv.gsfc.nasa.gov>
  6. Keywords: allocs, multidim arrays, and FAQ
  7. Sender: usenet@nsisrv.gsfc.nasa.gov (Usenet)
  8. Nntp-Posting-Host: ame.gsfc.nasa.gov
  9. Organization: NASA Goddard
  10. Date: Thu, 31 Dec 1992 17:03:07 GMT
  11. Lines: 114
  12.  
  13. Recently I attempted to create a small fragment of C
  14. using the FAQ in the newsgroup.  Here is the fragment
  15. from:
  16.  
  17. 1) The Newsgroup
  18.  
  19. 2.10:    How can I dynamically allocate a multidimensional array?
  20.  
  21. A:    It is usually best to allocate an array of pointers, and then
  22.     initialize each pointer to a dynamically-allocated "row."  The
  23.     resulting "ragged" array can save space, although it is not
  24.     necessarily contiguous in memory as a real array would be.  Here
  25.     is a two-dimensional example:
  26.  
  27.         int **array = (int **)malloc(nrows * sizeof(int *));
  28.         for(i = 0; i < nrows; i++)
  29.             array[i] = (int *)malloc(ncolumns * sizeof(int));
  30.  
  31.     (In "real" code, of course, malloc would be declared correctly,
  32.     and each return value checked.)
  33.  
  34.     You can keep the array's contents contiguous, while making later
  35.     reallocation of individual rows difficult, with a bit of
  36.     explicit pointer arithmetic:
  37.  
  38.         int **array = (int **)malloc(nrows * sizeof(int *));
  39.         array[0] = (int *)malloc(nrows * ncolumns * sizeof(int));
  40.         for(i = 1; i < nrows; i++)
  41.             array[i] = array[0] + i * ncolumns;
  42.  
  43.     In either case, the elements of the dynamic array can be
  44.     accessed with normal-looking array subscripts: array[i][j].
  45.  
  46. (My goal is to make an array accessible as array[i][j] on the fly)
  47.  
  48. 2) The Program I wrote (I am new to C programming, don't laugh <: )
  49.    (Implements the code above).
  50.  
  51. #include <stdio.h>
  52.  
  53. main ()
  54. {
  55.  
  56. int i,j;
  57.  
  58. int nrows, ncolumns;
  59.  
  60. ncolumns = 10;
  61.  
  62. printf ("Enter array index: \n");
  63. scanf ("%d", &nrows);
  64.  
  65.  
  66. int **array = (int **) malloc (nrows * sizeof(int *));
  67. for (i=0; i < nrows; i++)
  68.   array[i] = (int *) malloc (ncolumns * sizeof(int *));
  69.  
  70. printf ("Reading array values, and printing\n");
  71. for (i=0; i<10; i++)
  72.   for (j=0; j<nrows; j++)
  73.   {
  74.     printf("%d x %d = %d\n",i,j,i*j);
  75.     array[i][j] = i*j;
  76.     printf("%d,%d = %d\n",i,j,array[i][j]);
  77.  
  78.   }
  79.  
  80. }
  81.  
  82. 3) The program above is supposed to create a 2-dimensional array
  83.    with one dimension already specified, one dimension user specified,
  84.    and then it creates a multiplication table (pretty simple).
  85.    Here are the compile errors I received:
  86.  
  87. accom: Error: malloctest.c, line 16: syntax error
  88.        int **array = (int **) malloc (nrows * sizeof(int *));
  89.        --^
  90. accom: Error: malloctest.c, line 18: array undefined
  91.          array[i] = (int *) malloc (ncolumns * sizeof(int *));
  92.        ---------^
  93. accom: Error: malloctest.c, line 18: illegal indirection
  94.          array[i] = (int *) malloc (ncolumns * sizeof(int *));
  95.        ---------^
  96. accom: Error: malloctest.c, line 18: illegal combination of pointer and integer, op =
  97.          array[i] = (int *) malloc (ncolumns * sizeof(int *));
  98.        ------------------------------------------------------^
  99. accom: Error: malloctest.c, line 18: types :   int  versus   pointer to int
  100.          array[i] = (int *) malloc (ncolumns * sizeof(int *));
  101.        ------------------------------------------------------^
  102. accom: Error: malloctest.c, line 25: illegal indirection
  103.            array[i][j] = i*j;
  104.        -----------^
  105. accom: Error: malloctest.c, line 25: illegal indirection
  106.            array[i][j] = i*j;
  107.        --------------^
  108. accom: Error: malloctest.c, line 26: illegal indirection
  109.            printf("%d,%d = %d\n",i,j,array[i][j]);
  110.        -------------------------------------^
  111. accom: Error: malloctest.c, line 26: illegal indirection
  112.            printf("%d,%d = %d\n",i,j,array[i][j]);
  113.        ----------------------------------------^
  114.  
  115. My apologies about the length of the problem, but I wished to make
  116. the problem well stated so I can possibly get a good answer.
  117. Where is the error, the FAQ code, or my code or something that
  118. is machine dependent?  (Im using UNIX off of an SGI).
  119.  
  120. Email replies to almeida@ame.gsfc.nasa.gov, or post them here,
  121. Im sure that some of us could use a little more elaboration
  122. on use of the multidim arrays and allocation on the fly.
  123.  
  124. Thanks!
  125.  
  126. A. Almeida
  127.