2D array of pointers to 2D arrays 
Author Message
 2D array of pointers to 2D arrays

Hi Group!

I have 256 2D arrays of intgers on the form Nx2
like the ones below. Now i want a 2D array 16 x 16 with pointers
to these arrays.  Anyone have any id how to do this.

const int g1s1[][2]=
{
  {1,2},
  {3,4}

Quote:
};

const int g1s2[][2]=
{
  {1,2},
  {3,4},
  {5,6}

Quote:
};

.
.
.
const int g16s16[][2]=
{
.
.
.

Quote:
};

const int ptrArray[16][16]=  /* This is the thing i have trouble with */
{                            /* How do i write this correct ?*/
  {g1s1,g1s2,.....,g1s16},
  {g2s1,..........,g2s16},
             .
             .
             .
  {g16s16,........,g216};

Quote:
};

/Erik Cato

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Fri, 04 Apr 2003 03:00:00 GMT  
 2D array of pointers to 2D arrays

Quote:

> I have 256 2D arrays of intgers on the form Nx2
> like the ones below. Now i want a 2D array 16 x 16 with pointers
> to these arrays.  Anyone have any id how to do this.

<snip>

If you are prepared to restructure your code slightly, you could
keep pointers to your original arrays, and have an extra level
of dereference in your accessor code.

If you don't mind losing the const-ness of the large array,
you could construct it in the code by memcpy()'ing the small
arrays into the large array. (Remember, even in 2D arrays,
all elements must be continuous.)

With some help from a code generator, you could have two
independent copies built from the same data. (Write a
program which generates C source.)

Bill, can't find a pub that's open at 11 AM on a Saturday.



Fri, 04 Apr 2003 03:00:00 GMT  
 2D array of pointers to 2D arrays

Quote:

> Hi Group!

> I have 256 2D arrays of intgers on the form Nx2
> like the ones below. Now i want a 2D array 16 x 16 with pointers
> to these arrays.  Anyone have any id how to do this.

> const int g1s1[][2]=
> {
>   {1,2},
>   {3,4}
> };

> const int g1s2[][2]=
> {
>   {1,2},
>   {3,4},
>   {5,6}
> };

> .
> .
> .
> const int g16s16[][2]=
> {
> .
> .
> .
> };

> const int ptrArray[16][16]=  /* This is the thing i have trouble with */
> {                            /* How do i write this correct ?*/
>   {g1s1,g1s2,.....,g1s16},
>   {g2s1,..........,g2s16},
>              .
>              .
>              .
>   {g16s16,........,g216};
> };

The basic idea looks OK. Your problem is in the type
declaration of ptrArray

Here's a simplified example of a 2 X 2 array of pointers
to small arrays of N X 2

arr.c------------------------
#include <stdlib.h>
#include <stdio.h>

const int a11[][2] ={ {1100,1101} };
const int a12[][2] ={ {1200,1201},
                      {1210,1211} };
const int a21[][2] ={ {2100,2101},
                      {2110,2111},
                      {2120,2121} };
const int a22[][2] ={ {2200,2201},  
                      {2210,2211},
                      {2220,2221},  
                      {2230,2231} };

const int (* const aa[2][2])[2] = { {a11,a12},
                                    {a21,a22} };

int main(void)
{
    int i0,i1,i2,cnt;
    cnt=0;
    for(i0=0;i0<2;i0++)
    {
        for(i1=0;i1<2;i1++)
        {
            for(i2=0;i2<=cnt;i2++)
            {
                printf("%5d %6d\n"
                    ,aa[i0][i1][i2][0],aa[i0][i1][i2][1]);
            }
            printf("\n");  
            cnt++;
        }
    }
    return EXIT_SUCCESS;

Quote:
}

----------------------------------------------
On execution gives:-
 1100   1101

 1200   1201
 1210   1211

 2100   2101
 2110   2111
 2120   2121

 2200   2201
 2210   2211
 2220   2221
 2230   2231
-----------------------------------------------
Strictly speaking aa is an array of pointers
to 2 position 1D arrays -- not to 2D arrays.
With variable N in in the first dimension of
the original 2D arrays it would be difficult
to do otherwise.

Hope this helps,

Malcolm Kay



Fri, 04 Apr 2003 22:38:17 GMT  
 2D array of pointers to 2D arrays


Quote:

> > Hi Group!

> > I have 256 2D arrays of intgers on the form Nx2
> > like the ones below. Now i want a 2D array 16 x 16 with pointers
> > to these arrays.  Anyone have any id how to do this.

> > const int g1s1[][2]=
> > {
> >   {1,2},
> >   {3,4}
> > };

> > const int g1s2[][2]=
> > {
> >   {1,2},
> >   {3,4},
> >   {5,6}
> > };

> > .
> > .
> > .
> > const int g16s16[][2]=
> > {
> > .
> > .
> > .
> > };

> > const int ptrArray[16][16]=  /* This is the thing i have trouble
with */
> > {                            /* How do i write this correct ?*/
> >   {g1s1,g1s2,.....,g1s16},
> >   {g2s1,..........,g2s16},
> >              .
> >              .
> >              .
> >   {g16s16,........,g216};
> > };

> The basic idea looks OK. Your problem is in the type
> declaration of ptrArray

> Here's a simplified example of a 2 X 2 array of pointers
> to small arrays of N X 2

> arr.c------------------------
> #include <stdlib.h>
> #include <stdio.h>

> const int a11[][2] ={ {1100,1101} };
> const int a12[][2] ={ {1200,1201},
>                       {1210,1211} };
> const int a21[][2] ={ {2100,2101},
>                       {2110,2111},
>                       {2120,2121} };
> const int a22[][2] ={ {2200,2201},
>                       {2210,2211},
>                       {2220,2221},
>                       {2230,2231} };

> const int (* const aa[2][2])[2] = { {a11,a12},
>                                     {a21,a22} };

Thank u for that answer, but could u explain why
it looks like that.

I think that this would be more correct:
const int (* const aa[2][2])[][2] =
{
  {a11,a12},
  {a21,a22}

Quote:
};

(a const 2x2 array of pointers to const X x 2 array of int.)

i guess its not right but why?
Can anyone please help me to understand
the mysterious world of pointers!

- Show quoted text -

Quote:

> int main(void)
> {
>     int i0,i1,i2,cnt;
>     cnt=0;
>     for(i0=0;i0<2;i0++)
>     {
>         for(i1=0;i1<2;i1++)
>         {
>             for(i2=0;i2<=cnt;i2++)
>             {
>                 printf("%5d %6d\n"
>                     ,aa[i0][i1][i2][0],aa[i0][i1][i2][1]);
>             }
>             printf("\n");
>             cnt++;
>         }
>     }
>     return EXIT_SUCCESS;
> }
> ----------------------------------------------
> On execution gives:-
>  1100   1101

>  1200   1201
>  1210   1211

>  2100   2101
>  2110   2111
>  2120   2121

>  2200   2201
>  2210   2211
>  2220   2221
>  2230   2231
> -----------------------------------------------
> Strictly speaking aa is an array of pointers
> to 2 position 1D arrays -- not to 2D arrays.
> With variable N in in the first dimension of
> the original 2D arrays it would be difficult
> to do otherwise.

> Hope this helps,

> Malcolm Kay

Sent via Deja.com http://www.deja.com/
Before you buy.


Sat, 05 Apr 2003 14:24:55 GMT  
 2D array of pointers to 2D arrays

Quote:



[snip]
> > const int (* const aa[2][2])[2] = { {a11,a12},
> >                                     {a21,a22} };

> Thank u for that answer, but could u explain why
> it looks like that.

> I think that this would be more correct:
> const int (* const aa[2][2])[][2] =
> {
>   {a11,a12},
>   {a21,a22}
> };

> (a const 2x2 array of pointers to const X x 2 array of int.)

> i guess its not right but why?
> Can anyone please help me to understand
> the mysterious world of pointers!

Let us start with a single pointer members of a 1D array:-

The array:
  int a[4] = { 0,1,2,3 };
The pointer:
  int * pa = a;

Now *pa returns the value of a[0]
and *(pa+2) returns the value of a[2]
By definition the expression *(pa+2) is equivalent to pa[2]
and this later form is generally more convenient even if
not so intuitive.

Notice replaced a clear dereferencing expression *(pa+2)
with an expression in array style syntax pa[2]

------------------------------------------------------------
Now let us move on to say 4 of 1D arrays:
  int a0[2];
  int a1[1];
  int a2[4] = { 0,1,2,3 };
  int a3[3];
Now we'll need 4 pointers which can be held in an array:
  int *pan[4] ={ a0, a1, a2, a3 };
Now to access the value of say a2[0] we could do this though
the pointer pan[2] which points to a2[0] using
  *pan[2]
Notice pan[2] is a pointer so it needs dereferencing with
the * operator.
Alternatively we can do the dereferencing with [] as in
  pan[2][0]

Now to access the value of a2[2] via pan in the most straight
forward many we would use:
  *(pan[2]+2)
Or more conveniently
  pan[2][2]

Notice if we don't use the dereferencing operator *
we need an extra pair [] to do the dereferencing beyond
those used in the declaration of pan.

In your case elements of the pointer array are not pointers
to int as in these simplified cases but pointers to single
dimension arrays of 2 int members.

I think perhaps by now you will catch on.

Malcolm Kay



Sat, 05 Apr 2003 03:00:00 GMT  
 2D array of pointers to 2D arrays

Quote:

>Hi Group!

>I have 256 2D arrays of intgers on the form Nx2
>like the ones below. Now i want a 2D array 16 x 16 with pointers
>to these arrays.  Anyone have any id how to do this.

>const int g1s1[][2]=
>{
>  {1,2},
>  {3,4}
>};

>const int g1s2[][2]=
>{
>  {1,2},
>  {3,4},
>  {5,6}
>};

>.
>.
>.
>const int g16s16[][2]=
>{
>.
>.
>.
>};

>const int ptrArray[16][16]=  /* This is the thing i have trouble with */
>{                            /* How do i write this correct ?*/
>  {g1s1,g1s2,.....,g1s16},
>  {g2s1,..........,g2s16},
>             .
>             .
>             .
>  {g16s16,........,g216};
>};

>/Erik Cato

>Sent via Deja.com http://www.deja.com/
>Before you buy.

Build it from the ground up:

        const static int g1s1[][2] = {{1,2},{3,4},{5,6}};
        static const int g1s2[][2] = {{11,12},{13,14}};
        typedef const int int_2[2];
        typedef int_2 *ptr_2;
        ptr_2 ptrArray[16][16] = {{g1s1,g1s2},{g1s2}};

Note that the arrays must be static to be used in an initializer.

<<Remove the del for email>>



Sun, 06 Apr 2003 09:13:38 GMT  
 
 [ 6 post ] 

 Relevant Pages 

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

2. use 1d array + macro or 2d array?

3. Multiply and Add matrices with 2d array WITHOUT ARRAY INDEXING

4. How To pass 2D String array to VB from VC++ Using Safe array

5. Fast array transfer with 2D arrays...?

6. Help : Pointer to 2D Array

7. char pointer to 2D char array

8. Pointers to 2D arrays

9. converting 2D array to pointer

10. a dynamical 2D array to a double pointer

11. Pointer to 2D array...

12. HOWTO:2d array of pointers to structure

 

 
Powered by phpBB® Forum Software