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: Returning multidimensional array from a proc ? - comp.lang.tcl ...Hi, I have multidimensional array in a proc and which to return it. I.e Is ... because array_name is not a temporary array*/ return ... array to a proc fcmp function ... Copying rows in a two dimensional array. - comp.lang.ada ...... why Ada does not allow slicing in multidimensional arrays. ... function First_Row (Instance : Instance_Type ... this cannot be used as modifiable actual argument ... how to access multi-dimensional array input in C mex file - comp ...argument of type "double" is incompatible with parameter of type ..... double ... Multidimensional array (3D Array) in C Programming Language C allows array of two or more ... Help needed: read 3-dimensional array from a MAT-file in Fortran ...... rem General parameters rem ... needed: read 3-dimensional array from a MAT-file in Fortran ..... function ... namelist input of multidimensional array? - comp ... New features added to development gawk - comp.lang.awk... accepts an optional fourth argument which if supplied is an array ... we could get TAWK's true-multidimensional arrays in ... and the values in a 2-d array values: function ... multidimensional matrix multiplication - comp.soft-sys.matlab ...multidimensional matrix multiplication - comp.soft-sys.matlab ... ... comp.graphics.api.opengl NEWELM - using sigmoid function ... The data has to be in the form of a matrix (2D array ... Local array variables in functions - comp.lang.awkArray parameters passed to functions are passed 'by reference' in awk, and changing an array inside the function will change its origin outside the fu... Re: Finding local maxima in multidimensional array (efficiently ...... line_, array_] := >>> Extract[array, line]> >>> Max[Extract[array, >>> Select[Map[Function[s, s ... Position In Matrix (not Array ... is a multidimensional array ... bitmap as array - comp.graphics.api.openglIf you look on how C defines how multidimensional arrays are to be setup in memory, then it ... In Level 2 m-file s-function, can the dimension of vector Dwork be ... bitmap ... cross product between vector and array - comp.soft-sys.matlab ...Hi, I have a vector, V, and an array, A. Which function in MATLAB running the ... Help rapresenting multidimensional array ( pivot table ) - comp ... cross product ... multidimensional array as a function argument - C Boardmultidimensional array as a function argument. This is a discussion on multidimensional array as a function argument within the C++ Programming forums, part of the ... Dynamic Multidimensional Arrays As Function Arguments | C Plus Plusdynamic multidimensional arrays as function arguments C++ (Non Visual C++ Issues) 7/26/2012 8:46:21 PM
|