home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- From: nikki@trmphrst.demon.co.uk (Nikki Locke)
- Path: sparky!uunet!pipex!demon!trmphrst.demon.co.uk!nikki
- Subject: Re: Passing 2-d arrys to functions
- Reply-To: nikki@trmphrst.demon.co.uk
- References: <C0Hw4n.Hy9@knot.ccs.queensu.ca> <C0H9sA.BGw@newsserver.technet.sg> <24568@alice.att.com>
- Distribution: world
- X-Mailer: cppnews $Revision: 1.30 $
- Organization: Trumphurst Ltd.
- Lines: 66
- Date: Fri, 8 Jan 1993 12:26:28 +0000
- Message-ID: <726521188snx@trmphrst.demon.co.uk>
- Sender: usenet@demon.co.uk
-
- In article <C0Hw4n.Hy9@knot.ccs.queensu.ca> liu@vision(John Liu) writes:
- > I am trying to learn C++ and have a problem in passing a 2-d matrix to and
- > from a function. Specifically, the problem is as follows:
- >
- > main()
- > {
- > double a[2][2];
- > ......
- > func(a);
- > }
- >
- > void func(double b[][]){
- > ......
- > }
- >
- > When I tried to compile it, I got error message that the dimension of the matrix
- > b is null. However, I did not have any problem in passing any 1-d array
- > using the same way. I am working on SUN stations in our department.
-
- In C (and C++), an array is passed to a function as the address of its
- first element. Not other information (like the size of the array) is
- passed.
-
- For a single-dimension array, this is fine - all the compiler needs to
- know to access the n'th element is the address of the first element, and
- the size of each element (which it can deduce from the type). The
- calculation is simply (n * sizeof(element)).
-
- For a two (or more) dimensional array, to access element [m][n], the
- compiler needs to know the address of the first element, the size of each
- element, and the number of elements in the least significant dimension.
- The calculation for an array declared [DIM1][DIM2] is
- ((m * DIM2 + n) * sizeof(element)).
-
- Thus, when declaring the array function parameter, all but the most
- significant (left-most) dimension must be specified at compile time.
-
- In your example, you need ...
- > void func(double b[][2]){
- > ......
- > }
-
- There is nothing to stop you doing ...
- > void func(double b[2][2]){
- > ......
- > }
- if you wish.
-
- The confusion between pointers and arrays in C (and C++) is one of the
- tricky bits. Fervent C++ purists have even been know to say that array
- handling in C++ is completely broken (because it is like that in C), and
- should rarely be used.
-
- You might well find it useful to obtain the FAQ list for C and C++. I
- don't know where the C one is to be found, but the C++ one can be obtained ...
-
- Via ftp - Anonymous ftp from sun.soe.clarkson.edu [128.153.12.3] :pub/C++/FAQ
-
- Via mail - send mail
- To: archive-server@sun.soe.clarkson.edu
- Subject: send C++/FAQ
-
- Many thanks to Marshall Cline for compiling and maintaining it.
- --
- Nikki Locke,Trumphurst Ltd.(PC and Unix consultancy) nikki@trmphrst.demon.co.uk
- trmphrst.demon.co.uk is NOT affiliated with ANY other sites at demon.co.uk.
-