C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Multidimensional arrays as function arguments

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Ron
Guest





PostPosted: Fri Dec 12, 2003 9:46 am    Post subject: Multidimensional arrays as function arguments Reply with quote



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! ]
Back to top
Francis Glassborow
Guest





PostPosted: Fri Dec 12, 2003 6:28 pm    Post subject: Re: Multidimensional arrays as function arguments Reply with quote



In article <c668c1f8.0312111557.6840a742 (AT) posting (DOT) google.com>, Ron
<rcrane (AT) ictv (DOT) com> writes
Quote:
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! ]

Back to top
Bogdan
Guest





PostPosted: Sat Dec 13, 2003 2:12 am    Post subject: Re: Multidimensional arrays as function arguments Reply with quote



[email]rcrane (AT) ictv (DOT) com[/email] (Ron) wrote in message news:<c668c1f8.0312111557.6840a742 (AT) posting (DOT) google.com>...
Quote:
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! ]

Back to top
Ron
Guest





PostPosted: Sat Dec 13, 2003 11:23 am    Post subject: Re: Multidimensional arrays as function arguments Reply with quote

Quote:
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! ]

Back to top
Francis Glassborow
Guest





PostPosted: Sat Dec 13, 2003 3:41 pm    Post subject: Re: Multidimensional arrays as function arguments Reply with quote

In article <c668c1f8.0312122328.18c472de (AT) posting (DOT) google.com>, Ron
<rcrane (AT) ictv (DOT) com> writes
Quote:
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! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.