How to access a 3-dimensional array using an array pointer

I declared a pointer to a group from a 3-dimensional array, which I divided below. I have a problem accessing elements of a 3-dimensional array using pointers to the 3rd array.

#include <stdio.h>

void main()
{
   int m,row,col;
   int *ptr,*j;
   int array[2][5][2]={10,20,30,40,50,60,70,80,90,100,18,21,3,4,5,6,7,81,9,11};
   int (*p)[5][2];   //  pointer to an group of 3-d array
   p=array;
   for(m=0;m<2;m++)
   {
      ptr=p+m;
      for(row=0;row<5;row++)
      {
         ptr=ptr+row;
         for(col=0;col<2;col++)
         {
            printf("\n the vale is %d",*(ptr+col));
         }
      }
   }
}

exit:

 the value is 10
 the value is 20
 the value is 20
 the value is 30
 the value is 40
 the value is 50
 the value is 70
 the value is 80
 the value is 18
 the value is 21
 the value is 18
 the value is 21
 the value is 21
 the value is 3
 the value is 4
 the value is 5
 the value is 7
 the value is 81
 the value is -1074542408
 the value is 134513849

My question is: how to access the elements of a 3-dimensional array using a pointer to an array, and in my case the output shows my code not accessing elements 90, 100, 9, 11 and how can I access this in the above code . Thanks in advance.

+4
source share
4 answers

, 1- , , , , , .

#include <stdio.h>

/* 1 */
#define TABLES  2
#define ROWS    5
#define COLS    2

/* 2 */
int main()
{
    /* 3 */
    int array[TABLES][ROWS][COLS] = {
                                        { {10, 20}, {30, 40}, {50, 60}, {70, 80}, {90, 100} },
                                        { {18, 21}, {3, 4}, {5, 6}, {7, 81}, {9, 11} }
                                    };
    /* pointer to the first "table" level - array is 3-d but decays into 2-d giving out int (*)[5][2] */
    /* name your variables meaningully */
    int (*table_ptr)[ROWS][COLS] = array;  /* try to club up declaration with initialization when you can */
    /* 4 */
    size_t i = 0, j = 0, k = 0;
    for (i = 0; i < TABLES; ++i)
    {
        /* pointer to the second row level - *table_ptr is a 2-d array which decays into a 1-d array */
        int (*row_ptr)[COLS] = *table_ptr++;
        for (j = 0; j < ROWS; ++j)
        {
            /* pointer to the third col level - *row_ptr is a 1-d array which decays into a simple pointer */
            int *col_ptr = *row_ptr++;
            for (k = 0; k < COLS; ++k)
            {
                printf("(%lu, %lu, %lu): %u\n", (unsigned long) i, (unsigned long) j, (unsigned long) k, *col_ptr++);  /* dereference, get the value and move the pointer by one unit (int) */
            }         
        }
    }
    return 0;   /* report successful exit status to the platform */
}

,

ptr=p+m; GCC assignment from incompatible pointer type; p int (*)[5][2], ( 5) ( 2) , ptr, . Intead, int (*ptr) [5];, ptr = *(p + m);. , ( p table_ptr), , m, p.

( ) int *x ( col_ptr), int *x = *(ptr + m1). , : int (*) [5][2], int (*) [2] int *. table_ptr, row_ptr col_ptr.

+7

p .

#include <stdio.h>

void main()
{
   int m,row,col;
   int array[2][5][2]={10,20,30,40,50,60,70,80,90,100,18,21,3,4,5,6,7,81,9,11};
   int (*p)[5][2];   //  pointer to an group of 3-d array
   p=array;
   for(m=0;m<2;m++)
   {
      for(row=0;row<5;row++)
      {         
         for(col=0;col<2;col++)
         {
            printf("\n the vale is %d", *((int*)(p+m) + (row*2) + col));
         }         
      }
   }
}
+2

You can easily access all elements by simply looping through 2*5*2 = 20and using a pointer to the first element of the array, i.e. array[0][0][0]assuming the 3D array is 1D array of arrays of arrays of int.

#include <stdio.h>

void main()
{
   int m; //row,col;
   int *ptr; //,*j;
   int array[2][5][2]={10,20,30,40,50,60,70,80,90,100,18,21,3,4,5,6,7,81,9,11};
   //int (*p)[5][2];   //  pointer to an group of 3-d array
   //p=array;
   ptr = &array[0][0][0];
   for(m=0;m <2;m++)
   {
        for (m = 0; m < 20; m++)
      /* ptr=ptr+m;
      for(row = 0;row < 5;row ++)
      {
         ptr=ptr+row;
         for(col=0;col<2;col++)
         {
            printf("\n the vale is %d",*(ptr+col));
         }
      }*/
      printf("\n the vale is %d", *(ptr++));
   }
} 

I commented on some parts of your code and left it in the modified code so that you understand what I did.

+1
source
#include<stdio.h>


int main()
{
int array[2][2][2]={1,2,3,4,5,6,7,8};

 int *p;
p=&array[0][0][0];


int i=0,j,k;

 /*Accessing data using pointers*/

     for(i=0;i<2;i++)
        {
          for(j=0;j<2;j++)
            {

              for(k=0;k<2;k++)
               {
               printf("%d\n",*p);
                p++;
               }
            }

         }

  return 0;

}

This is sample code where elements in a 2X2X2 array are accessible using pointers. Hope this helps !!!!

+1
source

All Articles