Topic: const 2D array a valid subroutine formal argument?


Author: vavasis@cs.cornell.edu (Stephen Vavasis)
Date: Fri, 24 Jun 1994 19:30:52 GMT
Raw View
I have a subroutine that takes a 2D array of doubles.  The size of the
array is known at compile-time.  I want the subroutine to "promise"
that it will not change the entries of the array.  Is this possible
using "const" in C++?  Clearly it is possible for a 1D array.  I tried
to use the analogous syntax for 2D arrays:

   void mysubroutine(const double a[][5]) {
      // subroutine statements don't change entries of a.
   }
   main() {
     double b[5][5];
     mysubroutine(a);
     // etc
   }

The Sun 3.0.2 compiler ("cfront") accepted this code.  The Sun 4.0
compiler rejected the type conversion from b to a.  I am wondering
what the "C++ standard" says about this setup.  I am also wondering:
if it's not legal, is there a different way for the subroutine to
promise that it won't change the entries of the 2D array?

I've enclosed the paragraph from the Sun 4.0 release notes that seems
to explain why the Sun 4.0 compiler rejects the above program.  I
don't really understand why this paragraph should apply to 2D arrays.
(After all, 2D arrays aren't "really" pointers to pointers, and I
can't see how you could violate const safety with what I am trying to
do.)

Thanks,
Steve Vavasis (vavasis@cs.cornell.edu)