简体   繁体   中英

C: accessing multidimensional arrays with pointers

I have the following code which works for 2-dimensional arrays using two methods shown. It also works for 3-dimension arrays only a single method.

Does anyone know what the pointer based solution, similar to int *p = ((int *) mda1) + (3 * r) + c; , would look like for the 3-dimensional array? Thanks

#include <stdio.h>

int main(int argc, char *argv[])
{
    int mda1[2][3] = {{0, 1, 2}, {3, 4, 5}};
    int mda2[][3] = {{0, 1, 2}, {3, 4, 5}};
    int mda3[2][2][1] = { {{0}, {1}},
                          {{2}, {3}} };

    for (int r=0; r<2; r++) {
        for (int c=0; c<3; c++) {
            int *p = ((int *) mda1) + (3 * r) + c;
            printf("%d", *p);
        }
    }
    printf("\n");

    int (*mda1p)[3] = mda1;
    for (int i=0; i<2; ++i) {
        for (int j=0; j<3; ++j)
            printf("%d", *(*(mda1p + i) + j));
    }
    printf("\n");


    int (*mda3p)[2][1] = mda3;
    for (int i=0; i<2; ++i) {
        for (int j=0; j<2; ++j) {
            for (int k=0; k<1; ++k) {
                printf("%d", *(*(*(mda3p + i) + j) + k));
            }
        }
    }

    return 0;
}

SOLUTION:

#define MDA3_X 2
#define MDA3_Y 2
#define MDA3_Z 1
     int mda3[MDA3_X][MDA3_Y][MDA3_Z] = { {{0}, {1}},
                      {{2}, {3}} };

     for (int i=0; i<MDA3_X; ++i) {
          for (int j=0; j<MDA3_Y; ++j) {
               for (int k=0; k<MDA3_Z; ++k) {
                    printf("%d",
               *((int *) mda3 +
                 MDA3_Y * MDA3_Z * i + MDA3_Z * j + k));
               }
          }
     }
     printf("\n");

If I have understood correctly you are trying to write something like

int *p = ( int * )mda3;
printf( "%d", *( p + 2 * 1 * i + 1 * j + k ) );

That is in general if you have a three-dimensional array declared like

int a[N1][N2][N3];

then using a pointer of the type int * you can write

int *p = ( int * )a;

//...

printf( "%d", *( p + N2 * N3 * i + N3 * j + k ) );

Here is a demonstrative program.

#include <stdio.h>

int main(void) 
{
    enum { N1 = 2, N2 = 3, N3 = 4 };
    int a[N1][N2][N3] =
    {
        {
            {  1,  2,  3,  4 },
            {  5,  6,  7,  8 },
            { 10, 11, 12, 13 }
        },
        {
            {  21, 22, 23, 24 },
            {  25, 26, 27, 28 },
            {  30, 31, 32, 33 }
        }
    };

    int *p = ( int * )a;
    puts( "a =" );
    printf( "{\n" );
    for ( size_t i = 0; i < N1; i++ )
    {
        printf( "\t{\n" );
        for ( size_t j = 0; j < N2; j++ )
        {
            printf( "\t\t{" );
            for ( size_t k = 0; k < N3; k++)
            {
                printf( "%2d ", *( p + N2 * N3 *i + N3 * j + k ) );
            }
            printf( "}\n" );
        }
        printf( "\t}\n" );

    }
    printf( "}\n" );

    return 0;
}

Its output is

a =
{
    {
        { 1  2  3  4 }
        { 5  6  7  8 }
        {10 11 12 13 }
    }
    {
        {21 22 23 24 }
        {25 26 27 28 }
        {30 31 32 33 }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM