Multidimensional arrays as function arguments

  • Follow


I have some legacy code that uses multidimensional arrays, which I
need to pass to various functions* I'd like to pass them as const
arguments to make it clear that these function shouldn't modify them.
I know how this works for 2d arrays (s.4.4(4)):

   char *someArray[20];
   ...
   void f (const char * const *a, int missingDimSize) {...}
   ...
   f (someArray, 20);

However, I haven't been able to come up with the right list of const
qualifiers to make it work with 3d arrays:

   char *someArray[20][30];
   ...
   void f (const char * const (* a) [30], int missingDimSize); // ???
   ...
   f (someArray, 20);  // compiler says "incompatible pointer types"

* I wish I could change this code to use nested std::vector <>s, but
no dice.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply rcrane 12/12/2003 9:46:01 AM

In article <c668c1f8.0312111557.6840a742@posting.google.com>, Ron
<rcrane@ictv.com> writes
>I have some legacy code that uses multidimensional arrays, which I
>need to pass to various functions* I'd like to pass them as const
>arguments to make it clear that these function shouldn't modify them.
>I know how this works for 2d arrays (s.4.4(4)):

consider:

int const dim1(30);
int const dim2(40);
int const dim3(5);
typedef int array3d[dim1][dim2][dim3];

void foo(array3d const &);

In C++ we can pass arrays around by reference provided we have fixed
dimensions. You could even use a function template:

#include <iostream>

template<int d1, int d2, int d3>
void foo(int const ( &ar )[d1][d2][d3]){std::cout << ar[0][0][0];}


int main(){
  int a[3][4][5] = {1,2,3};
  foo<3,4,5>(a);
}
However watch out for code bloat.

-- 
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
                            or http://www.robinton.demon.co.uk


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Francis 12/12/2003 6:28:56 PM


rcrane@ictv.com (Ron) wrote in message news:<c668c1f8.0312111557.6840a742@posting.google.com>...
> I have some legacy code that uses multidimensional arrays, which I
> need to pass to various functions* I'd like to pass them as const
> arguments to make it clear that these function shouldn't modify them.
> I know how this works for 2d arrays (s.4.4(4)):
> 
>    char *someArray[20];
>    ...
>    void f (const char * const *a, int missingDimSize) {...}
>    ...
>    f (someArray, 20);
> 
> However, I haven't been able to come up with the right list of const
> qualifiers to make it work with 3d arrays:
> 
>    char *someArray[20][30];
>    ...
>    void f (const char * const (* a) [30], int missingDimSize); // ???
>    ...
>    f (someArray, 20);  // compiler says "incompatible pointer types"
> 
> * I wish I could change this code to use nested std::vector <>s, but
> no dice.
> 
Hi,

VC++6 says: "Types pointed to are unrelated; conversion requires
reinterpret_cast, C-style cast or function-style cast"

Try this:
 f( (const char * const (*)[30])someArray, 20 );


Regards,
Bogdan

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply bogy01 12/13/2003 2:12:06 AM

 > >I have some legacy code that uses multidimensional arrays, which I
 > >need to pass to various functions* I'd like to pass them as const
 > >arguments to make it clear that these function shouldn't modify them.
 > >I know how this works for 2d arrays (s.4.4(4)):
 >
 > consider:
 >
 > int const dim1(30);
 > int const dim2(40);
 > int const dim3(5);
 > typedef int array3d[dim1][dim2][dim3];
 >
 > void foo(array3d const &);
 >
 > In C++ we can pass arrays around by reference provided we have fixed
 > dimensions....

Hmm, that sounds good. But is there any way to do it without using
references, as with code that needs also to be compilable with a "C"
compiler?

-Ron

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply rcrane 12/13/2003 11:23:46 AM

In article <c668c1f8.0312122328.18c472de@posting.google.com>, Ron 
<rcrane@ictv.com> writes
> > >I have some legacy code that uses multidimensional arrays, which I
> > >need to pass to various functions* I'd like to pass them as const
> > >arguments to make it clear that these function shouldn't modify them.
> > >I know how this works for 2d arrays (s.4.4(4)):
> >
> > consider:
> >
> > int const dim1(30);
> > int const dim2(40);
> > int const dim3(5);
> > typedef int array3d[dim1][dim2][dim3];
> >
> > void foo(array3d const &);
> >
> > In C++ we can pass arrays around by reference provided we have fixed
> > dimensions....
>
>Hmm, that sounds good. But is there any way to do it without using
>references, as with code that needs also to be compilable with a "C"
>compiler?

Of course, and very simply:

void foo(array3d const * const);

The only other change is that my declarations of dim1, dim2 and dim3 
become either #defines or:

enum dimensions{first = 30, second = 40, third = 5};

For reasons of portability while retaining at least a semblance of scope 
sensitivity I prefer the latter.


-- 
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
                             or http://www.robinton.demon.co.uk


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Francis 12/13/2003 3:41:11 PM

4 Replies
390 Views

(page loaded in 0.002 seconds)

Similiar Articles:













7/26/2012 8:46:21 PM


Reply: