home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19323 < prev    next >
Encoding:
Text File  |  1993-01-06  |  3.0 KB  |  114 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!zaphod.mps.ohio-state.edu!cs.utexas.edu!sun-barr!ames!nsisrv!ame.gsfc.nasa.gov!almeida
  3. From: almeida@ame.gsfc.nasa.gov (aswin m. almeida)
  4. Subject:  The 2-Dee Array Deelemna
  5. Message-ID: <1993Jan6.190355.9632@nsisrv.gsfc.nasa.gov>
  6. Sender: usenet@nsisrv.gsfc.nasa.gov (Usenet)
  7. Nntp-Posting-Host: ame.gsfc.nasa.gov
  8. Organization: NASA Goddard
  9. Date: Wed, 6 Jan 1993 19:03:55 GMT
  10. Lines: 102
  11.  
  12. I would like to thank those of you who helped me earlier
  13. with my buggy malloctest program that was an attempt at
  14. declaring a two dimensional array on the fly.  Using
  15. your suggestions and corrections, I came up with the
  16. following fragment of code, which runs, and then gets
  17. a bus error (I am curious as to what is causing this
  18. bus error).
  19.  
  20.  
  21. PROGRAM: (Corrected as by your mail to me)
  22.  
  23.  
  24. In addition, I would greatly appreciate it if someone
  25. could explain (with crude ASCII drawings) *, **, and &.
  26. I find it easiest to learn with drawings when it comes
  27. to pointers (its how I learned PASCAL), but C is a 
  28. new cup of tea, and a picture is worth a thousand words <: 
  29.  
  30.  
  31. more malloctest.c
  32. #include <stdio.h>
  33.  
  34. main ()
  35. {
  36.  
  37. int i,j;
  38.  
  39. int nrows, ncolumns;
  40. int **array;               /* 1) corrected, moved from 2) below */
  41.  
  42. ncolumns = 10;
  43.  
  44. printf ("Enter array index: \n");
  45. scanf ("%d", &nrows);
  46.  
  47.  
  48. array = (int **) malloc (nrows * sizeof(int *)); /* 2) int declaration moved to 1) */
  49. for (i=0; i < nrows; i++)
  50.   array[i] = (int *) malloc (ncolumns * sizeof(int));  /* removed * before last int */
  51.  
  52. printf ("Reading array values, and printing\n");
  53. for (i=0; i<10; i++)
  54.   for (j=0; j<nrows; j++)
  55.   {
  56.     printf("%d x %d = %d\n",i,j,i*j);
  57.     array[i][j] = i*j;   /* dbx reports this as the location of the bus error */
  58.     printf("%d,%d = %d\n",i,j,array[i][j]);
  59.  
  60.   }
  61.  
  62. }
  63.  
  64. Ok, basically, this is the program.  I moved int **array up with the other declarations,
  65. as you all told me to do, and removed a * before the last int (I would greatly
  66. appreciate a good explanation of why this is so).
  67.  
  68. Here is the input/output:
  69.  
  70. a.out
  71. Enter array index: 
  72. 3
  73. Reading array values, and printing
  74. 0 x 0 = 0
  75. 0,0 = 0
  76. 0 x 1 = 0
  77. 0,1 = 0
  78. 0 x 2 = 0
  79. 0,2 = 0
  80. 1 x 0 = 0
  81. 1,0 = 0
  82. 1 x 1 = 1
  83. 1,1 = 1
  84. 1 x 2 = 2
  85. 1,2 = 2
  86. 2 x 0 = 0
  87. 2,0 = 0
  88. 2 x 1 = 2
  89. 2,1 = 2
  90. 2 x 2 = 4
  91. 2,2 = 4
  92. 3 x 0 = 0
  93. Bus error (core dumped)
  94.  
  95. What gives here?
  96.  
  97. Again, I would like to thank those of you who responded and helped with 
  98. debugging this small fragment of code.  Now I am looking for
  99.  
  100. 1) The reason I am getting this annoying bus error.
  101. 2) An explanation, with crude ASCII pictures, of *, **, and & in C.
  102. 3) An explanation of why | array[i] = (int *) malloc (ncolumns * sizeof(int));
  103.    I was told to remove the star here ----------------------------------^ before int.
  104.  
  105. I have a few better C books now (went to Crown and picked Crash Course in C
  106. and a Microsoft C book by McGrawHill), and I also have the C bible, though
  107. I am not a computer jock (yet <: ) and its not really for me.  I'd
  108. appreciate any comments or suggestions directly to 
  109.  
  110. almeida@ame.gsfc.nasa.gov
  111.  
  112. Thanks!
  113. A. Almeida
  114.