如何使用指向数组的指针访问3-d数组

tam*_*nov 4 c arrays pointers

我已经声明了一个指向一组3-d数组的指针,我在下面分享过.我在使用指向3-d数组的指针访问3-d数组的元素时遇到了问题.

#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));
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

输出:

 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
Run Code Online (Sandbox Code Playgroud)

我的问题是如何使用指向数组的指针访问三维数组的元素,在我的情况下输出显示我的代码不访问元素90,100,9,11以及如何在上面的代码中访问它.谢谢预先.

leg*_*s2k 9

尽管扁平化数组并将它们作为一维数组访问是可能的,因为您的原始问题是使用指向内部维度的指针,这里是一个答案,它使用数组衰减行为为您提供每个级别的指针.

#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 */
}
Run Code Online (Sandbox Code Playgroud)

内联代码注释详细说明参考

  1. 将维度定义在某处并在其他地方使用它是一种很好的做法; 在一个地方改变会在所有地方改变它并避免讨厌的错误
  2. main的retrun类型是int而不是void
  3. 建议不避内支架
  4. 使用size_t持有大小类型

代码中的问题

对于线路ptr=p+m;,GCC抛出assignment from incompatible pointer type; reason是p类型,int (*)[5][2]即指向整数数组(大小为2)的数组(大小为5)的指针,它被分配给ptr它只是一个整数指针.Intead如果你把它改成int (*ptr) [5];然后做ptr = *(p + m);.这就是我的代码所做的(我已经命名ptable_ptr),只是它不使用m但它p直接递增.

在第三级(最内层循环)之后,你需要一个整数指针说int *x(在我的代码中这是col_ptr)你要做的int *x = *(ptr + m1).Bascially你需要有三个不同的指针,每一个级别:int (*) [5][2],int (*) [2]int *.我把它们命名了table_ptr,row_ptr并且col_ptr.

  • +1用于给出第一个链接。谢谢。 (2认同)
  • @haccks:不客气。我是最近才发现的,但似乎对您有很大帮助,所以我将其传递给了:) (2认同)

hac*_*cks 1

您只需循环2*5*2 = 20并使用指向数组第一个元素的指针即可轻松访问所有元素,即array[0][0][0]假设 3D 数组为 1Darray 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++));
   }
} 
Run Code Online (Sandbox Code Playgroud)

我对你的代码的某些部分进行了注释,并将其保留在修改后的代码中,以便让你清楚我做了什么。