pointer arithmetic with 2d arrays? 
Author Message
 pointer arithmetic with 2d arrays?

Quote:

> Hello...

> I'm trying to do pointer arithmetic with 2d arrays.  Example:

> int arr[15][15];

> call_func(arr) = array initialization...;

> int call_func ( int arr[][15] )
> {
>         arr = arr + 1;    /* arr is now arr[1][0] */
>         printf("%d", **arr);    /* prints data contained in
>                 arr[1][0] */
> }

> how would i use pointer arithmetic to access the nth element of
> arr[1]... like arr[1][4]?  i've tried many different methods, but none

Not sure why do you want to do that. arr[i][j] is the syntax suger
to make your life easier.

Anyway, here is one way to do that:
int* p = &arr[0][0];
*(p + i*15 + j) = ... /* same as arr[i][j] = ... */



Sat, 21 Apr 2001 03:00:00 GMT  
 pointer arithmetic with 2d arrays?
Hello...

I'm trying to do pointer arithmetic with 2d arrays.  Example:

int arr[15][15];

call_func(arr) = array initialization...;

int call_func ( int arr[][15] )
{
        arr = arr + 1;    /* arr is now arr[1][0] */
        printf("%d", **arr);    /* prints data contained in
                arr[1][0] */

Quote:
}

how would i use pointer arithmetic to access the nth element of
arr[1]... like arr[1][4]?  i've tried many different methods, but none
work... thanks for any help.


Sun, 22 Apr 2001 03:00:00 GMT  
 pointer arithmetic with 2d arrays?

Quote:

> I'm trying to do pointer arithmetic with 2d arrays.  Example:

> int arr[15][15];

> call_func(arr) = array initialization...;

> int call_func ( int arr[][15] )
> {
>    arr = arr + 1;    /* arr is now arr[1][0] */
>    printf("%d", **arr);    /* prints data contained in
>            arr[1][0] */
> }

> how would i use pointer arithmetic to access the nth element of
> arr[1]... like arr[1][4]?  i've tried many different methods, but none
> work... thanks for any help.

If

        foo a[][dim2][dim3][dim4]
        foo a[dim1][dim2][dim3][dim4]

is an array of foo[d2][d3][d4] or pointer to foo[d2][d3][d4], then

         a[i1][i2][i3][i4]

is equivalent to

   ((foo*)a)[i1*dim2*dim3*dim4 + i2*dim3*dim4 + i3*dim4 + i4]

 = ((foo*)a)[((i1*dim2+i2)*dim3+i3)*dim4 + i4]

etc. I hope the rule is self-explanatory.

In your case

        int a[][dim2];

        a[i][j] resolves to

        ((int*)a)[i*d2+j] = *( (int*)a + i*d2 + j )

In fact that's what your compiler does behind the curtain.

Regards
Horst



Sun, 22 Apr 2001 03:00:00 GMT  
 pointer arithmetic with 2d arrays?

Quote:

>Hello...

>I'm trying to do pointer arithmetic with 2d arrays.  Example:

>int arr[15][15];

>call_func(arr) = array initialization...;

>int call_func ( int arr[][15] )
>{
>        arr = arr + 1;    /* arr is now arr[1][0] */

Not exactly. Lets create a separate variable to avoid confusion (remove the
line above).

         int (*arr2)[15] = arr;

After

         arr2++;   /* or ++arr2 or arr2 += 1 or arr2 = arr2 +1 */

then arr2 is now arr+1 or &arr[1].

*arr2 would be *(arr+1) or arr[1] or &arr[1][0].

**arr2 would be **(arr+1) or *arr[1] or arr[1][0]

Quote:
>        printf("%d", **arr);    /* prints data contained in
>                arr[1][0] */
>}

>how would i use pointer arithmetic to access the nth element of
>arr[1]... like arr[1][4]?  i've tried many different methods, but none
>work... thanks for any help.

The first thing to realise is that the [] notation is a form of pointer
arithmetic i.e. p[i] is defined as *(p + i). [] always takes a pointer and
an integer operand. So if you want arr[1][4] you can say just that with the
[] notation specifying the correct pointer arithmetic. Given the definition
of [] above this is equivalemtn to (*(arr+1))[4] or *(*(arr+1)+4). Since
arr2 is arr+1 that would be (*arr2)[4] or *(*arr2 + 4).

However that looks somewhat complex and it can be shown more clearly using
a separate pointers for the row and column. Consider this:

void call_func ( int arr[][15] )
{
    int (*rowp)[15];
    int *colp;

    rowp = arr + 1;     /* rowp now points to the second row */
    colp = *rowp + 4;   /* colp now points to the 5th column on the 2nd row */

    printf("%d", *colp);    /* prints data contained in arr[1][4] */

Note that colp is pointing to an element in a row i.e. an element of an
array of int so it has type pointer to int.

--
-----------------------------------------


-----------------------------------------



Sun, 22 Apr 2001 03:00:00 GMT  
 pointer arithmetic with 2d arrays?

Quote:





>> > Hello...

>> > I'm trying to do pointer arithmetic with 2d arrays.  Example:

>> > int arr[15][15];

>> > call_func(arr) = array initialization...;

>> > int call_func ( int arr[][15] )
>> > {
>> >     arr = arr + 1;    /* arr is now arr[1][0] */
>> >     printf("%d", **arr);    /* prints data contained in
>> >             arr[1][0] */
>> > }

>> > how would i use pointer arithmetic to access the nth element of
>> > arr[1]... like arr[1][4]?  i've tried many different methods, but none
>> > work... thanks for any help.

>> Pointer arithmetic with multidimensional array is one of the most obscure
>> point of C. Adetailed discussion is reported in the C-FAQ at
>> http://www.eskimo.com/~scs/C-faq/top.h. INHO I'd discourage using too smart
>> technique on multidimensional array. If it is of any use the correct syntax
>> for printing a[1][4] is printf("%d\n", *(*(array+1)+4) ); Not very clear,
>                                         ^^^^^^^^^^^^^^^ No it's not, this
>will cause the program to attempt to load a pointer from (array+1). For an
>array of arrays the syntax is

Assuming a, arr and array are referring to the same thing here then
arr[1][4] is by definition equivalent to *(*(arr+1)+4). arr+1 generates
a pointer to the 2nd element of arr i.e. the 2nd array of 15 ints.
*(arr+1) accesses what the pointer points at i.e. the 2nd array of 15 ints
This array is then used as an operand of the outer + so it is converted
to a pointer to the first element of the array. 4 is then added to this
pointer value and the result is dereferenced.

Quote:
>   *(array + (index_1 * dimension_2) + index_2)

>so for printing a[1][4] is printf( "%d\n", *(array + (1 * 15) + 4) );

That might be true if you were trying to simulate access to a 2 dimensional
array using a plain int * pointer. However arr/array here has type int (*)[15]
and pointer arithmetic works in units of the element type (or type
pointed at) which here is an array of 15 ints.

--
-----------------------------------------


-----------------------------------------



Mon, 23 Apr 2001 03:00:00 GMT  
 pointer arithmetic with 2d arrays?

Quote:


>>> > Hello...

>>> > I'm trying to do pointer arithmetic with 2d arrays.  Example:

>>> > int arr[15][15];

>>> > call_func(arr) = array initialization...;

>>> > int call_func ( int arr[][15] )
>>> > {
>>> >     arr = arr + 1;    /* arr is now arr[1][0] */
>>> >     printf("%d", **arr);    /* prints data contained in
>>> >             arr[1][0] */
>>> > }

>>> > how would i use pointer arithmetic to access the nth element of
>>> > arr[1]... like arr[1][4]?  i've tried many different methods, but none
>>> > work... thanks for any help.

As someone who has extensive experience in programming and C, I have only
one question:  Why?


Fri, 27 Apr 2001 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. 2D array of pointers to 2D arrays

2. Which 2D array of 2D array addressing method?

3. Managed arrays and pointer arithmetic

4. Help : Pointer to 2D Array

5. char pointer to 2D char array

6. Pointers to 2D arrays

7. converting 2D array to pointer

8. a dynamical 2D array to a double pointer

9. arrays of strings & possible pointer arithmetic

10. 3D arrays and pointer arithmetic

11. Pointer to 2D array...

12. HOWTO:2d array of pointers to structure

 

 
Powered by phpBB® Forum Software