[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

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

Cuthbert

3/18/2011 4:42:00 AM

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
4 Answers

China Blue Veins

3/18/2011 5:29:00 AM

0

In article <e0dc3b80-b72e-4ba1-aa11-b3c530b95924@18g2000prd.googlegroups.com>,
Cuthbert <cuthbert.kao@gmail.com> wrote:

> 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.

Because you're lying to the compiler. access_ary is ((u16*)[]) and thus can only
take two subscripts, not three. I don't know of any straightforward way to
encode your intention. Unfortunately the C type system was designed to be clever
rather than orthogonal, so you can't express every possible concept in the
language.

>
> 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

int main(int n,char **p) {
printf("%d\n",access_ary[0][2*2+1]); /*do the subscript computation by hand*/
return 0;
}

/ cc t.c
t.c:18: warning: initialization from incompatible pointer type
t.c:19: warning: initialization from incompatible pointer type
/ a.out
777

--
Damn the living - It's a lovely life. I'm whoever you want me to be.
Silver silverware - Where is the love? At least I can stay in character.
Oval swimming pool - Where is the love? Annoying Usenet one post at a time.
Damn the living - It's a lovely life. Alameda County Sheriff.

jt

3/18/2011 10:33:00 AM

0

Cuthbert <cuthbert.kao@gmail.com> 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 ___ jt@toerring.de
\__________________________ http://t...

Ben Bacarisse

3/18/2011 11:33:00 AM

0

Cuthbert <cuthbert.kao@gmail.com> 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.

James Dow Allen

3/18/2011 11:35:00 AM

0

On Mar 18, 11:42 am, Cuthbert <cuthbert....@gmail.com> wrote:
> 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".)

----
James Dow Allen