# How could I use a pinter array to manipulate several 2D arrays?

Discussion in 'C Programming' started by Cuthbert, Mar 18, 2011.

1. ### CuthbertGuest

Hi guys,

I am trying to use a single array to access multiple 2D arrays but
I kept getting the error message. Could someone help me on this? Thank
you very much.

Here is my code:
// Definition
u16 const_1[4][2] ={
{25,65},
{23,65},
{2,777},
{2,13}
};

u16 const_2[4][2] ={
{15,65},
{13,65},
{1,5},
{1,13}
};

u16 *access_ary[2] = {
const_1,
const_2};

...............
// access them
k = access_ary[0][2][1]; // expecting k=777

Cuthbert, Mar 18, 2011

2. ### Jens Thoms ToerringGuest

Cuthbert <> wrote:
> I am trying to use a single array to access multiple 2D arrays but
> I kept getting the error message.

Would be useful if you'd cite "the error message"...

> Could someone help me on this? Thank
> you very much.

> Here is my code:
> // Definition
> u16 const_1[4][2] ={
> {25,65},
> {23,65},
> {2,777},
> {2,13}
> };

> u16 const_2[4][2] ={
> {15,65},
> {13,65},
> {1,5},
> {1,13}
> };

> u16 *access_ary[2] = {
> const_1,
> const_2};

This won't do - 'access_ary' is defined as an array of two pointers
to u16, not pointers to 2-dimensional arrays...

> ..............
> // access them
> k = access_ary[0][2][1]; // expecting k=777

This is the next best thing I can come up with on short
notice (replace 'int' with 'u16' as necessary):

#include <stdio.h>

typedef int Arr2D [ 4 ][ 2 ];

Arr2D const_1 = { { 25, 65 },
{ 23, 65 },
{ 2, 777 },
{ 2, 13 } };

Arr2D const_2 = { { 15, 65 },
{ 13, 65 },
{ 1, 5 },
{ 1, 13 } };

Arr2D *access_ary[ 2 ] = { &const_1, &const_2 };

int main( )
{
printf( "%d\n", ( *access_ary[ 0 ] )[ 2 ][ 1 ] );
return 0;
}

You can do without the typedef, of course, but then things
will look a lot less readable;-) Using that typedef it's
straightforward to define an array of pointers to 4x2
arrays. Accessing the elements of the arrays pointed to
isn't as simple as you may wish for but then more-dimen-
sional arrays are a bit tricky in C and mixing them with
pointers doesn't make things easier...

Regards, Jens
--
\ Jens Thoms Toerring ___
\__________________________ http://toerring.de

Jens Thoms Toerring, Mar 18, 2011

3. ### Ben BacarisseGuest

Cuthbert <> writes:
<snip>
> Here is my code:
> // Definition
> u16 const_1[4][2] ={
> {25,65},
> {23,65},
> {2,777},
> {2,13}
> };
>
> u16 const_2[4][2] ={
> {15,65},
> {13,65},
> {1,5},
> {1,13}
> };
>
> u16 *access_ary[2] = {
> const_1,
> const_2};

As already stated, you need to have an array of array pointers here.
Rather than get into the details, I want to ask what you are aiming for.
Why, for example, do you not simply have a 3D array? Do you expect to
change the pointers in access_ary at run-time? Are you looking ahead
and hoping to solve some code maintenance problem by building up the
data structure like this? There may be another way to get where you
want to go.

--
Ben.

Ben Bacarisse, Mar 18, 2011
4. ### James Dow AllenGuest

On Mar 18, 11:42 am, Cuthbert <> wrote:
> Could someone help me on this?
> u16 *access_ary[2] = {

Replace this line with
u16 (*access_ary[2])[2] = {

but obfuscate the fact that only a single line in
OP source was "wrong".)

----
James Dow Allen

James Dow Allen, Mar 18, 2011