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

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!cs.utexas.edu!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!ira.uka.de!smurf.sub.org!easix!bc3!ktf
  3. From: ktf@bc3.GUN.de (Klaus ter Fehn)
  4. Subject: Re: The 2-Dee Array Deelemna
  5. Organization: private
  6. Date: Thu, 7 Jan 1993 17:30:11 GMT
  7. Message-ID: <C0HvAD.3Gu@bc3.GUN.de>
  8. References: <1993Jan6.190355.9632@nsisrv.gsfc.nasa.gov>
  9. Lines: 139
  10.  
  11.  
  12.  
  13. In comp.lang.c you write:
  14.  
  15. >In addition, I would greatly appreciate it if someone
  16. >could explain (with crude ASCII drawings) *, **, and &.
  17. >I find it easiest to learn with drawings when it comes
  18. >to pointers (its how I learned PASCAL), but C is a 
  19. >new cup of tea, and a picture is worth a thousand words <: 
  20.  
  21. >more malloctest.c
  22. >#include <stdio.h>
  23.  
  24. >main ()
  25. >{
  26.  
  27. >int i,j;
  28.  
  29. >int nrows, ncolumns;
  30. >int **array;               /* 1) corrected, moved from 2) below */
  31.  
  32. >ncolumns = 10;
  33.  
  34. >printf ("Enter array index: \n");
  35. >scanf ("%d", &nrows);
  36.  
  37.  
  38. >array = (int **) malloc (nrows * sizeof(int *)); /* 2) int declaration moved to 1) */
  39. >for (i=0; i < nrows; i++)
  40. >  array[i] = (int *) malloc (ncolumns * sizeof(int));  /* removed * before last int */
  41.  
  42. >printf ("Reading array values, and printing\n");
  43.  
  44. Your first index [i] is counting rows and the second [j] the columns
  45. so change the loop:
  46.  
  47. > /* for (i=0; i<10; i++) */      for (i = 0; i < nrows; i++)
  48. > /*  for (j=0; j<nrows; j++) */   for (j = 0; j < ncolumns; j++)
  49. >  {
  50. >    printf("%d x %d = %d\n",i,j,i*j);
  51. >    array[i][j] = i*j;   /* dbx reports this as the location of the bus error */
  52. >    printf("%d,%d = %d\n",i,j,array[i][j]);
  53.  
  54. >  }
  55.  
  56. >}
  57.  
  58. >Ok, basically, this is the program.  I moved int **array up with the other declarations,
  59. >as you all told me to do, and removed a * before the last int (I would greatly
  60. >appreciate a good explanation of why this is so).
  61.  
  62. (...)
  63.  
  64. >Now I am looking for
  65.  
  66. >1) The reason I am getting this annoying bus error.
  67. >2) An explanation, with crude ASCII pictures, of *, **, and & in C.
  68. >3) An explanation of why | array[i] = (int *) malloc (ncolumns * sizeof(int));
  69. >   I was told to remove the star here ----------------------------------^ before int.
  70.  
  71. 1) See the changes I made in your code above (the for-loops)
  72.    You mixed up i and j / rows and columns and perhaps writing to an
  73.    memory location, you were not allowed to access.
  74.  
  75. 2) I take your example:
  76.  
  77. I assume nrows is 2 and ncolumns is 3 (otherwise it'll be too much to draw
  78. within vi :-)
  79.  
  80. int **array    
  81.   ,---.
  82.   | o |    A pointer to a location, where a integer-pointer resides
  83.   `-+-' 
  84.      \
  85.       _|
  86.     ,---.      Two pointers to location, where integer-values reside.
  87.     [0] | o-+      The two integer-pointers, you malloc()ed space for in your
  88.     +---+ \    first malloc():
  89.     [1] | o |  |   array = (int **) malloc (nrows * sizeof(int *));
  90.     `-+-'  |  
  91.           |     \ 
  92.           |      _| [0][0] [0][1] [0][2]
  93.       |        ,------+------+------.  The two areas to store the integer-
  94.       |        |      |      |      |  values, you malloc()ed with your
  95.       |        `------+------+------'  second malloc() (in the loop)
  96.          \|/
  97.      ,------+------+------.
  98.      |      |      |      |
  99.      `------+------+------'
  100.       [1][0] [1][1] [1][2]
  101.  
  102. An other example to show the meanings of **, * and &:
  103.  
  104.     int value, *pointer, **doublepointer;
  105.  
  106.     value = 777;
  107.     pointer = &value;
  108.     doublepointer = &pointer;
  109.  
  110. Let us assume, that the vars lie on the following locations:
  111.  
  112.     varname    memory location  stored value
  113.     value        1000        777
  114.     pointer        1004        1000
  115.     doublepointer    1008        1004
  116.  
  117. Then the following expressions have the shown results:
  118.  
  119. expression    result    meaning
  120. &value        1000    adress of 'value'
  121. value        777     value of 'value'
  122. &pointer    1004    adress of 'pointer'
  123. pointer        1000    value of 'pointer'
  124. *pointer    777    value of adress where 'pointer' points to
  125. &doublepointer    1008    adress of 'doublepointer'
  126. doublepointer    1004    value of 'doublepointer'
  127. *doublepointer    1000    value of adress where 'doublepointer' points to
  128. **doublepointer    777     value of adress where the value of the memory 
  129.             location where 'doublepointer' points to, points to
  130.             (uhrg)
  131.  
  132.  
  133.  
  134. >3) An explanation of why | array[i] = (int *) malloc (ncolumns * sizeof(int));
  135. >   I was told to remove the star here ----------------------------------^ 
  136. >   before int.
  137.  
  138. Well - it would make no sense: you want to allocate ncolumns integer-values,
  139. so you got to allocate (ncolumns * sizeof(int)) bytes.
  140.  
  141. Also it will make no sense for the compiler. What is a (* int) ?!? If you
  142. intended to say 'integer-pointer' it would be (int *).
  143.  
  144. Greetings from Germany...
  145. -- 
  146. Klaus ter Fehn        <ktf@bc3.GUN.de>
  147. Neanderstr. 4        {mcshh,smurf,unido}!easix!bc3!ktf
  148. 4000 Duesseldorf 1
  149. FRG / Germany        Tel.: +49-211-676331
  150.