From: goodTweetieBird on
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[}[])
{
//???
}
From: Victor Bazarov on
goodTweetieBird wrote:
> 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[}[])

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
From: Igor Tandetnik on
"goodTweetieBird" <goodTweetieBird(a)hotmail.com> wrote in message
news:b27993ba-2d39-40c6-b56d-7c3dae869bc0(a)34g2000hsh.googlegroups.com
> int arr[20][30];
>
> main(int argc, char** argv)
> {
> int rval;
> ...
> rval = functionInOtherFile(arr);
> }
>
> //Other file here.
>
> int functionInOtherFile( int* arr[}[])

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


From: Ulrich Eckhardt on
goodTweetieBird wrote:
> int arr[20][30];
>
> main(int argc, char** argv)
> {
> int rval;
> ...
> rval = functionInOtherFile(arr);
> }
>
> //Other file here.
>
> int functionInOtherFile( int* arr[}[])

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
From: Victor Bazarov on
Victor Bazarov wrote:
> [..] 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)

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)

>
>> {
>> //???
>
> Once the array is passed in, you can use it just as like you would in
> the 'main' function:
>
> arr[17][3] = 42;
>
>> }
>
> [..]

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