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

C

Cuthbert

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
 
J

Jens Thoms Toerring

Cuthbert said:
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
 
B

Ben Bacarisse

Cuthbert said:
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.
 
J

James Dow Allen

Could someone help me on this?
u16 *access_ary[2] = {

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

(Other answers in the thread may convey more wisdom,
but obfuscate the fact that only a single line in
OP source was "wrong".)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top