Passing address of multidimentional array |
Igor Tandetnik posted on Tuesday, June 03, 2008 8:52 AM
|
You have to specify all but the leftmost dimension in the function
signature:
int functionInOtherFile( int arr[][30]);
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925 |
 |
|
Passing address of multidimentional array |
Ulrich Eckhardt posted on Tuesday, June 03, 2008 9:07 AM
|
Try using a typedef:
typedef int array_type[20][30];
void other_function( array_type const* p) {
for(int i=0; i!=30; ++i)
for(int j=0; j!=30; ++j)
print((*p)[i][j]);
}
int main() {
array_type arr;
other_function(&arr);
}
Of course, as already mentioned, you could pass a reference in C++.
Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932 |
 |
|
Passing address of multidimentional array |
Victor Bazarov posted on Tuesday, June 03, 2008 10:18 AM
|
Igor is right, I screwed it up (it's been a while since I worked with
multidimensional arrays directly in my code). The *left-most* can be
omitted, not the right-most.
int functionInOtherFile(int arr[][30], unsigned size)
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask |
 |
|
Passing address of multidimentional array |
Ron Francis posted on Tuesday, June 03, 2008 10:25 AM
|
Quote from Victor:
the C way to do it is to declare all dimensions except the last
(or the first, depends on how you count), and also provide the size of
the array:
int functionInOtherFile(int arr[20][], unsigned size)
Quote from Igor:
You have to specify all but the leftmost dimension in the function
signature:
int functionInOtherFile( int arr[][30]);
Now I'm confused, they are not both correct are they?
Ron |
 |
|
Passing address of multidimentional array |
Ron Francis posted on Tuesday, June 03, 2008 10:29 AM
|
Sorry Victor,
You corrected your post as I asked my question.
Cheers,
Ron. |
 |
|
Passing address of multidimentional array |
Doug Harrison [MVP] posted on Tuesday, June 03, 2008 11:36 AM
|
On Tue, 3 Jun 2008 05:36:22 -0700 (PDT), goodTweetieBird
Not sure what you meant to write there.
All the following are equivalent:
void f(int a[20][30]);
void f(int a[2][30]);
void f(int a[][30]);
void f(int (*a)[30]);
This is the *****only***** context in which array and pointer declaration
syntax is sort of interchangeable, and it's the reason the magnitude of the
first dimension doesn't matter - all four declare "a" to be a pointer to an
array of 30 ints. This has to be, because (ignoring C++ references) an
array passed to a function undergoes the array to pointer conversion, which
produces a pointer to its first element, so "a" has to be a pointer. For
your array int[20][30], this pointer has the type int(*)[30], or "pointer
to an array of 30 ints", and the number of these int[30] arrays is lost in
the conversion from array to pointer to first element. It's the same with
1D arrays; the pointer to the first element of an int[30] array is an int*,
and if all you have is the int*, there's no way to tell if it's a scalar or
an array, and no way to tell how big the latter is. No matter how you
declare the parameter "a", inside f, you index it as usual, e.g. a[0][0],
a[1][20], etc. As for the sort-of-interchanageable syntax mentioned
earlier, it's intended to ease the declaration of function parameters, but
it's the last declaration of "f" that expresses the underlying reality.
Don't think pointers and arrays are "the same"; they're not. Your array
int[20][30] is a solid block of ints and contains no pointers. All its
pointer-like characteristics stem from the array to pointer conversion.
--
Doug Harrison
Visual C++ MVP |
 |
|
Passing address of multidimentional array |
Cezary H. Noweta posted on Tuesday, June 03, 2008 3:25 PM
|
No - they are still different with all consequences. For example array
declarators (the first three) are not modifiable lvalues, while the last
declarator is.
So it is impossible to use ,,a = sth;''
-- best regards
Cezary Noweta |
 |
|
Passing address of multidimentional array |
Doug Harrison [MVP] posted on Tuesday, June 03, 2008 4:06 PM
|
It is as I said.
You are mistaken.
I do not know what that means.
--
Doug Harrison
Visual C++ MVP |
 |
|
Passing address of multidimentional array |
Victor Bazarov posted on Tuesday, June 03, 2008 4:14 PM
|
I think Cezary meant to imply that the assignment in 'foo' below is
supposed to fail for some reason:
void foo(int a[20][30])
{
int b[5][30];
a = b; // line 4
}
void bar(int (*a)[30])
{
int b[5][30];
a = b;
}
int main()
{
int arr[100][30];
foo(arr);
bar(arr);
}
Of course, since the two declarations are equivalent ('foo' and 'bar'
have the same type, essentially), there is no error on line 4.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask |
 |
|
Passing address of multidimentional array |
Cezary H. Noweta posted on Tuesday, June 03, 2008 4:48 PM
|
I agree ;) My prev post is stupid. 6.7.5.3[7] of ISO C99 states that ,,A
declaration of a parameter as ‘‘array of type’’ shall be adjusted to
‘‘qualified pointer to type’’, [...]''. I must go to bed and take some
sleep before my next postings ;)
-- best regards
Cezary Noweta |
 |
|
Passing address of multidimentional array |
goodTweetieBird posted on Wednesday, June 04, 2008 10:53 PM
|
I have an array defined in main.c and with to access it from a
function in another file preferrably using array indices but pointers
would be OK. I am not sure how to specify the function parameter or
how to index the elements via the foreign function. Would like a
little advice rather than proceeding blindly because I often find that
just because something compiles it is not necessarily correct.
Thanks,
gtb
~~~~~~~~~~~~~~~
int arr[20][30];
main(int argc, char** argv)
{
int rval;
...
rval = functionInOtherFile(arr);
}
//Other file here.
int functionInOtherFile( int* arr[}[])
{
//???
} |
 |
|
As always excellent advice.Thank you all.gtb |
goodTweetieBird posted on Wednesday, June 04, 2008 10:53 PM
|
As always excellent advice.
Thank you all.
gtb |
 |
|