pass a pointer to a multiple dimension array 
Author Message
 pass a pointer to a multiple dimension array

I have this
int poly[10][200][2];
and want to pass a pointer to this array to a function to modify the array
inside the function.

thanks,
Matias



Mon, 29 Aug 2005 14:19:40 GMT  
 pass a pointer to a multiple dimension array

Quote:

> I have this
> int poly[10][200][2];
> and want to pass a pointer to this array to a function to modify the array
> inside the function.

void modify_an_array(int arr[10][200][2]);

The above prototype will give you a function which takes a
pointer to an appropriately sized array.

Note that for example

void foo(int *arg)
void foo(int arg[])
void foo(int arg[10])

are all _exactly_ the same to the C language. It sinks in after
some thinking ;)

--
Thomas.



Mon, 29 Aug 2005 15:20:54 GMT  
 pass a pointer to a multiple dimension array

Quote:
> void modify_an_array(int arr[10][200][2]);

I did it that way, but I wanted to stay away from defining the index
(10,200,2) in the function header. That's why I asked if some kind of
notation works to avoid that

Something like
void modify_an_array(int* * * arr);

but that doesn't work

thanks,
Matias



Mon, 29 Aug 2005 22:57:36 GMT  
 pass a pointer to a multiple dimension array

Quote:

> > void modify_an_array(int arr[10][200][2]);

> I did it that way, but I wanted to stay away from defining the index
> (10,200,2) in the function header.

Impossible.  You can only omit the first index.

arr[x][y] is an array of 2 ints.
arr[x] is an array of 200 arrays of 2 ints.
So the compiler must know the dimentions [200] and [2] in order to know
where to find element arr[x][y][z]: the (z + 2*(y + 200*x))th element of
arr, counting from element#0.

--
Hallvard



Mon, 29 Aug 2005 23:32:29 GMT  
 pass a pointer to a multiple dimension array
Greetings:

Try the following code:

#include <stdio.h>
#include <stdlib.h>

f(int ***poly)
{
 int i, j, k;

 for (i=0; i<10; i++)
  for (j=0; j<200; j++)
   for (k=0; k<2; k++)
    printf("%d ", poly[i][j][k]);  /*will print everything
                                   in poly in the way you have
inited*/

Quote:
}

main()
{
 int ***poly;
 int i, j;

 poly = malloc(10*sizeof(int **));

 for (i=0; i<10; i++)
  poly[i] = malloc(200*sizeof(int *));

 for (i=0; i<10; i++)
  for (j=0; j<200; j++)
   poly[i][j] = malloc(2*sizeof(int));

 /*init the array to something*/

 f(poly)

Quote:
}

Please refer to the article "A tutorial on pointers and arrays in C"
by Ted Jensen (available in
various formats via http://home.netcom.com/~tjensen/ptr/cpoint.htm)
for a complete description of why this method works and much more. In
the end, you'll understand the exact relationship between arrays and
pointers (and multi-dimensional arrays and pointers to pointers to ...
pointers).

Regards,

Shuo

Quote:

> I have this
> int poly[10][200][2];
> and want to pass a pointer to this array to a function to modify the array
> inside the function.

> thanks,
> Matias



Mon, 29 Aug 2005 23:34:45 GMT  
 pass a pointer to a multiple dimension array

Quote:

> Greetings:

> Try the following code:

> #include <stdio.h>
> #include <stdlib.h>

> f(int ***poly)
> {
>  int i, j, k;

>  for (i=0; i<10; i++)
>   for (j=0; j<200; j++)
>    for (k=0; k<2; k++)
>     printf("%d ", poly[i][j][k]);  /*will print everything
>                                    in poly in the way you have
> inited*/
> }

> main()

Should be either int main() or int main(void) or something with argc.

Quote:
> {
>  int ***poly;
>  int i, j;

>  poly = malloc(10*sizeof(int **));

The clc dogmatic way of writing that is:
   poly = malloc(10 * sizeof *poly);

and you also have to take some sort of action if malloc fails:
    if (poly == NULL) {
        exit(EXIT_FAILURE);
    }
You can be more creative than that if you want to,
but you may not assume that malloc will not fail.

Quote:

>  for (i=0; i<10; i++)
>   poly[i] = malloc(200*sizeof(int *));

 Same goes for here, and all the other points of mallocation.

Quote:

>  for (i=0; i<10; i++)
>   for (j=0; j<200; j++)
>    poly[i][j] = malloc(2*sizeof(int));

>  /*init the array to something*/

>  f(poly)

You need a ; after f(poly)

For portability reasons,
the last byte to the standard output stream should be '\n'.

    putchar('\n');

main should return something.

    return 0;

- Show quoted text -

Quote:
> }

> Please refer to the article "A tutorial on pointers and arrays in C"
> by Ted Jensen (available in
> various formats via http://home.netcom.com/~tjensen/ptr/cpoint.htm)
> for a complete description of why this method works and much more. In
> the end, you'll understand the exact relationship between arrays and
> pointers (and multi-dimensional arrays and pointers to pointers to ...
> pointers).

> Regards,

> Shuo


> > I have this
> > int poly[10][200][2];
> > and want to pass a pointer to this array to a function to modify the array
> > inside the function.

> > thanks,
> > Matias

--
pete


Tue, 30 Aug 2005 08:20:08 GMT  
 pass a pointer to a multiple dimension array

Quote:

>> void modify_an_array(int arr[10][200][2]);

> I did it that way, but I wanted to stay away from defining the index
> (10,200,2) in the function header. That's why I asked if some kind of
> notation works to avoid that

> Something like
> void modify_an_array(int* * * arr);

> but that doesn't work

No, because the type "int a[10][200][2];" declares a as an array of 10
arrays of 200 arrays of 2 ints.  When you pass it to a function, it's
passed as a pointer to it's first element - which is an array of 200
arrays of 2 ints.

So, you can declare the function as:

    void modify_an_array(int (*arr)[200][2]);

or the completely equivalent:

    void modify_an_array(int arr[][200][2]);

When you try to use an expression (or sub-expression) like arr[n] in
your function, the compiler needs to know how big the elements of arr[n]
are, because it has to skip over n of them.  With the above
declarations, it knows this (200 * 2 * sizeof int).  If you had some way
of doing what you want, which is declaring arr as a pointer to an array
of unknown size of arrays of unknown size of ints, something like:

    void modify_an_array(int arr[][*][*]);   /* This is wrong. */

Then when you access arr[n], the compiler has no idea how find that
element, because it doesn't know how big the individual elements of
arr are.

        - Kevin



Tue, 30 Aug 2005 08:33:05 GMT  
 pass a pointer to a multiple dimension array

Quote:

> >> void modify_an_array(int arr[10][200][2]);
> > ... [versus] Something like
> > void modify_an_array(int* * * arr);
...
> No, because the type "int a[10][200][2];" declares a as an array of 10
> arrays of 200 arrays of 2 ints.  When you pass it to a function, it's
> passed as a pointer to it's first element - which is an array of 200
> arrays of 2 ints. ...

Yes.

Quote:
> When you try to use an expression (or sub-expression) like arr[n] in
> your function, the compiler needs to know how big the elements of arr[n]
> are, because it has to skip over n of them.  With the above
> declarations, it knows this (200 * 2 * sizeof int).

Yes through C89.

Quote:
> If you had some way
> of doing what you want, which is declaring arr as a pointer to an array
> of unknown size of arrays of unknown size of ints, something like:

>     void modify_an_array(int arr[][*][*]);   /* This is wrong. */

This is legal in C99, for a (nondefining) declaration only.
It declares the parameter to be a VLA type.

Quote:
> Then when you access arr[n], the compiler has no idea how find that
> element, because it doesn't know how big the individual elements of
> arr are.

The compiler doesn't know from this declaration, but it doesn't
need to compile a call.  The *definition* must use an expression,
but not just a constant expression as in C89, for the bound.
This expression is evaluated at function entry (or when a local
declaration is reached), and that bound is used throughout the
scope of the local entity (parameter or variable, or typedef).

It is usually desirable to make the variable bound be, or at least
depend on, other (preceding) parameters of the same function,
but that is not required.

--
- David.Thompson 1 now at worldnet.att.net



Fri, 02 Sep 2005 15:01:01 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Multiple dimensions arrays with MFC

2. passing a multi-dimension array (faq vs. gcc)

3. Calling C DLL From VB Help (Passing 2 Dimension Array)

4. Extending contiguous two dimension array to three dimensions?

5. about the 2-dimension array pointer

6. it is about the 2-dimension array pointer

7. pointer to 2-dimension array

8. pointer to multi dimension array

9. Passing pointers to arrays of pointers....

10. Passing Multiple Arrays to Functions

11. Passing multiple arrays by reference

12. How to get the 1st dimension of multi-dimension array from within the function it is passed to?

 

 
Powered by phpBB® Forum Software