home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / c / 19997 next >
Encoding:
Text File  |  1993-01-21  |  2.4 KB  |  98 lines

  1. Path: sparky!uunet!olivea!sgigate!sgiblab!munnari.oz.au!manuel.anu.edu.au!ampl1.anu.edu.au!stg121
  2. From: stg121@ampl1.anu.edu.au (Stephen Gibson)
  3. Newsgroups: comp.lang.c
  4. Subject: Dynamic Allocation of Matrices
  5. Message-ID: <1jleldINN23f@manuel.anu.edu.au>
  6. Date: 21 Jan 93 06:08:45 GMT
  7. References: <1993Jan19.194253.4100@ucc.su.OZ.AU> <1993Jan19.232122.20952@netcom.com> <TMB.93Jan21014916@arolla.idiap.ch>
  8. Reply-To: stg121@ampl1.anu.edu.au (Stephen Gibson)
  9. Organization: Australian National University
  10. Lines: 85
  11. NNTP-Posting-Host: 150.203.15.16
  12.  
  13. Dynamic allocation of matrices.
  14.  
  15. I would like to be able to generate arrays of various sizes and types
  16. by calling a function such as
  17.  
  18.       memory (array1,array2,array3);
  19.  
  20. where the prototype could be:
  21.  
  22.      void memory (double **,double **,int **);
  23.  
  24. From test cases, see below, it appears that only the following works:
  25.  
  26.      array1 = memory ();
  27.  
  28. What is the difference between 
  29.         double **function (void)    and   void function (double **) 
  30.  with respect to  double **  ?
  31.  
  32. Any helpful comments would be appreciated.
  33.                 
  34.                     Steve.
  35. -----------------------------------------------------------------------------
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <stddef.h>
  40.  
  41. double **memory (int m, int n)
  42. {
  43. /* case #1 - array returned from function */
  44.  int i;
  45.  double **array;
  46.  array = (double **) calloc (m,sizeof(double *));
  47.  for (i=0;i<m;i++) array[i] = (double *) calloc (n,sizeof(double));
  48.  return array;
  49. }
  50.  
  51. void memory2 (int m, int n, double **array)
  52. {
  53. /* case #2 - array passed as a function parameter */
  54.  int i;
  55.  array = (double **) calloc (m,sizeof(double *));
  56.  for (i=0;i<m;i++) array[i] = (double *) calloc (n,sizeof(double));
  57. }
  58.  
  59. void main ()
  60. {
  61.  int i,j,m,n;
  62.  double **a,**b;
  63.  
  64.  printf ("Enter array size m, n -> ");
  65.  scanf ("%d%d",&m,&n);
  66.  
  67.  printf ("Case #1\n"); 
  68.  a = memory (m,n);
  69.  for (i=0;i<m;i++) 
  70.   for (j=0;j<n;j++) a[i][j] = i+j+1;
  71.  for (i=0;i<m;i++) {
  72.   for (j=0;j<n;j++) printf (" %lg",a[i][j]);
  73.   printf ("\n");
  74.  }
  75.  
  76.  printf ("Case #2\n"); 
  77.  memory2 (m,n,b);
  78.  for (i=0;i<m;i++) 
  79.   for (j=0;j<n;j++) b[i][j] = i+j+1;
  80.  for (i=0;i<m;i++) {
  81.   for (j=0;j<n;j++) printf (" %lg",b[i][j]);
  82.   printf ("\n");
  83.  }
  84.  exit (EXIT_SUCCESS);
  85. }
  86.  
  87.  
  88. -------- compilation on DEC 5000/125 ---
  89. cc -o helpme helpme.c
  90. -------- execution ---------------------
  91. helpme
  92. Enter array size m, n -> 2 3
  93. Case #1
  94.  1 2 3
  95.  2 3 4
  96. Case #2
  97. Segmentation fault
  98.