home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18979 < prev    next >
Encoding:
Text File  |  1993-01-11  |  2.7 KB  |  69 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!microsoft!wingnut!pauljo
  3. From: pauljo@microsoft.com (Paul Johns)
  4. Subject: Re: Passing 2-d arrys to functions
  5. Message-ID: <1993Jan11.235754.23011@microsoft.com>
  6. Date: 11 Jan 93 23:57:54 GMT
  7. Organization: Microsoft Corp., Redmond, Washington, USA
  8. References: <1993Jan11.030238.6688@nuscc.nus.sg> <24568@alice.att.com> <726521188snx@trmphrst.demon.co.uk> <RUNE.93Jan10125255@pandora.nta.no>
  9. Distribution: usa
  10. Lines: 57
  11.  
  12. In article <1993Jan11.030238.6688@nuscc.nus.sg> suresh@argon.iss.nus.sg wrote:
  13.  
  14. > Yes, I think this program will work always .. since you have passed in 
  15. > a double ** pointer which is a vector of double*, the least dimension
  16. > of the array is not required to correctly index the values.
  17. > Although the same syntax can be used to access 2-d arrays (no flames
  18. > please .. I mean statically allocated as in double arr[2][2] ) and
  19. > variables of type pointer to pointer ( double **), the underlying memory 
  20. > layout need not be the same. However, the algorithm used to index either
  21. > one is the same.. pointer arithmetic. 
  22.  
  23. The algorithm is different.  Check out the assembly code generated to
  24. see how.
  25.  
  26. > I believe( correct me if I am wrong) most or all C/C++ compilers always
  27. > use the same pointer arithmetic no matter whether the array passed 
  28. > is declared as  double arr[][least_dim]  or  double **. 
  29.  
  30. This is absolutely not true.  The two pointers are of different types.
  31. The type for the array case is "double (*arr)[least_dim];".  The type
  32. in the pointer case is "double **arr;".  The addressing they use isn't
  33. the same.  (Show the result of arr++ in the two cases, or the type
  34. of *arr--to see this.)
  35.  
  36. To do "arr[3][4]" you need to do entirely different operations depending
  37. on the type of arr:
  38.  
  39. In the array case, you multiply the column number (3) by the row size 
  40. (least_dim) and add the row number (4), finally multiplying by the 
  41. element size (8) to find the offset of the element from the beginning
  42. of the two-dimensional array's block of contiguous memory.
  43.  
  44. In the pointer case, you index into the pointer array (using 3), 
  45. then use the address found there to index (using 4) into the one-
  46. dimensional array of doubles pointed to by the pointer in the
  47. pointer array.
  48.  
  49. > This explains
  50. > how we can successfully mix pointer arithmetic and array-indexing. 
  51. > The last dimension is required merely for setting up storage.
  52.  
  53. No.  This explains why you can't mix pointer operations.  Many have
  54. tried, and many have failed.
  55.  
  56. It **IS** ok to mix []'s and pointer arithmetic **IFF** the underlying
  57. pointer types are the same:
  58.  
  59.     Array type        Underlying pointer type
  60.  
  61.     char a[3];        char *a;
  62.     char a[3][5];        char (*a)[5];
  63.     char *a[4];        char **a;
  64.  
  65. The rule is to drop the leftmost subscript and bind the * operator
  66. tightly to the identifier.
  67.  
  68. // Paul
  69.