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

  1. Newsgroups: comp.lang.c++
  2. From: nikki@trmphrst.demon.co.uk (Nikki Locke)
  3. Path: sparky!uunet!pipex!demon!trmphrst.demon.co.uk!nikki
  4. Subject: Re: Passing 2-d arrys to functions
  5. Reply-To: nikki@trmphrst.demon.co.uk
  6. References: <C0Hw4n.Hy9@knot.ccs.queensu.ca> <C0H9sA.BGw@newsserver.technet.sg> <24568@alice.att.com>
  7. Distribution: world
  8. X-Mailer: cppnews $Revision: 1.30 $
  9. Organization: Trumphurst Ltd.
  10. Lines: 66
  11. Date: Fri, 8 Jan 1993 12:26:28 +0000
  12. Message-ID: <726521188snx@trmphrst.demon.co.uk>
  13. Sender: usenet@demon.co.uk
  14.  
  15. In article <C0Hw4n.Hy9@knot.ccs.queensu.ca> liu@vision(John Liu) writes:
  16. > I am trying to learn C++ and have a problem in passing a 2-d matrix to and 
  17. >    from a function. Specifically, the problem is as follows:
  18. >    main()
  19. >    {
  20. >    double a[2][2];
  21. >    ......
  22. >    func(a);
  23. >    }
  24. >    void func(double b[][]){
  25. >    ......
  26. >    }
  27. >    When I tried to compile it, I got error message that the dimension of the matrix 
  28. >    b is null. However, I did not have any problem in passing any 1-d array
  29. >    using the same way. I am working on SUN stations in our department.
  30.  
  31. In C (and C++), an array is passed to a function as the address of its 
  32. first element. Not other information (like the size of the array) is 
  33. passed.
  34.  
  35. For a single-dimension array, this is fine - all the compiler needs to 
  36. know to access the n'th element is the address of the first element, and 
  37. the size of each element (which it can deduce from the type). The 
  38. calculation is simply (n * sizeof(element)).
  39.  
  40. For a two (or more) dimensional array, to access element [m][n], the 
  41. compiler needs to know the address of the first element, the size of each 
  42. element, and the number of elements in the least significant dimension. 
  43. The calculation for an array declared [DIM1][DIM2] is 
  44. ((m * DIM2 + n) * sizeof(element)).
  45.  
  46. Thus, when declaring the array function parameter, all but the most 
  47. significant (left-most) dimension must be specified at compile time.
  48.  
  49. In your example, you need ...
  50. >    void func(double b[][2]){
  51. >    ......
  52. >    }
  53.  
  54. There is nothing to stop you doing ...
  55. >    void func(double b[2][2]){
  56. >    ......
  57. >    }
  58. if you wish.
  59.  
  60. The confusion between pointers and arrays in C (and C++) is one of the 
  61. tricky bits. Fervent C++ purists have even been know to say that array
  62. handling in C++ is completely broken (because it is like that in C), and
  63. should rarely be used. 
  64.  
  65. You might well find it useful to obtain the FAQ list for C and C++. I 
  66. don't know where the C one is to be found, but the C++ one can be obtained ...
  67.  
  68. Via ftp - Anonymous ftp from sun.soe.clarkson.edu [128.153.12.3] :pub/C++/FAQ
  69.  
  70. Via mail - send mail 
  71. To: archive-server@sun.soe.clarkson.edu
  72. Subject: send C++/FAQ
  73.  
  74. Many thanks to Marshall Cline for compiling and maintaining it. 
  75. -- 
  76. Nikki Locke,Trumphurst Ltd.(PC and Unix consultancy) nikki@trmphrst.demon.co.uk
  77. trmphrst.demon.co.uk is NOT affiliated with ANY other sites at demon.co.uk.
  78.