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

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

  1. Cuthbert

    Cuthbert Guest

    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
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. 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
    #3
  4. 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] = {

    (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
    James Dow Allen, Mar 18, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave
    Replies:
    1
    Views:
    310
    Mike Wahler
    Jan 22, 2005
  2. integer to pinter conversion

    , Sep 14, 2005, in forum: C Programming
    Replies:
    31
    Views:
    880
    Joe Wright
    Sep 20, 2005
  3. integer to pinter conversion

    , Sep 14, 2005, in forum: C Programming
    Replies:
    3
    Views:
    300
    Irrwahn Grausewitz
    Sep 14, 2005
  4. JoeC

    pinter = string.

    JoeC, Jun 21, 2007, in forum: C++
    Replies:
    6
    Views:
    335
    Jim Langston
    Jun 23, 2007
  5. Fab

    Reference to pinter

    Fab, May 29, 2013, in forum: C++
    Replies:
    3
    Views:
    169
    Marcel Müller
    May 30, 2013
Loading...

Share This Page