logo

Passing address of multidimentional array

Victor Bazarov posted on Tuesday, June 03, 2008 8:49 AM

First of all, your function has to be declared _before_ it's used, so
don't forget to add the declaration (directly or via an inclusion of
some header) to the 'main.c' file, before the 'main' function.  That
said, 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)


Once the array is passed in, you can use it just as like you would in
the 'main' function:

arr[17][3] = 42;


Now, if you were programming in C++, you could pass the array by reference.

Another way is to declare your array _extern_ and not pass it at all.
Of course, you can later decide that using the global data is a bad idea
(it is), and make the array a local object, then you're back to passing
it as the argument.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
reply


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
reply

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
reply

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
reply

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
reply

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.
reply

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
reply

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
reply

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
reply

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
reply

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
reply

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[}[])
{
//???
}
reply

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
reply

 

Didn't Find The Answer You Were Looking For?

View VC Language Posts   Ask A New Question

EggHeadCafe has experts online right now that may know the answer to your question.  We pay them a bonus for answering as many questions as they can.  So, why not help them and yourself by becoming a member (free) and ask them your question right now?
Ask Question In Live Forum

If you have an OpenID and do not want to become a member of the EggHeadCafe forum, you can also sign on to Chat Chaos and post your question to our real time Silverlight chat application.
Ask Question In Chat Chaos


Deployment Server    .NET Distributed Applications    .NET Framework    ADO.NET    ASP.NET    ASP.NET Security    ASP.NET Web Controls    ASP.NET Web Services    .NET CLR    .NET Compact Framework    .NET Drawing GDI+    .NET COM Interop    .NET Microframework    .NET Microframework Porting    .NET Performance    .NET Web Services    .NET Windows Forms    .NET WinForms Controls    .NET    C#    VB.NET    VC++.NET    .NET Security    Xml    JScript    VBScript    WSH    Smart Phone Developer    VB COM    VB Controls    VB Crystal    VB DataBase ADO    VB Syntax    VB Vista Compatibility    VB WinAPI    VC ATL    VC Debugger    VC Language    VC MFC    VC STL    Visio Developer VBA    Visual Studio .NET Debugging    Powershell    WindowsCE Embedded VC    Xml    Xsl   






  $1000 Contest    [)ia6l0 iii - $228  |  Jonathan VH - $161  |  Huggy Bear - $135  |  F Cali - $95  |  egg egg - $94  |  more Advertise  |  Privacy  |   (c) 2010