6

以下で共有している 3 次元配列のグループへのポインターを宣言しました。3 次元配列へのポインターを使用して 3 次元配列の要素にアクセスする際に問題があります。

#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));
         }
      }
   }
}

出力:

 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

私の質問は、配列へのポインターを使用して 3 次元配列の要素にアクセスする方法です。私の場合、出力は私のコードが要素 90,100,9,11 にアクセスしていないことを示しており、上記のコードでこれにアクセスするにはどうすればよいでしょうか。前進。

4

4 に答える 4

11

配列を平坦化し、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 */
}

参照で詳しく説明されたインライン コード コメント

  1. ディメンションをどこかで共通に定義し、それを別の場所で使用することをお勧めします。ある場所を変更すると、すべての場所が変更され、厄介なバグが回避されます
  2. main の再実行型は intであり、void ではありません
  3. 内側のブレースを避けないことをお勧めします
  4. size_tサイズタイプの保持に使用

コードの問題

行についてはptr=p+m;、GCC がスローしassignment from incompatible pointer typeます。理由は、整数の配列 (サイズ 2) の配列 (サイズ 5) へのポインタ、つまり整数ポインタである型ですp。に変更してから. これは私のコードが行うことです(私は と名付けました)、使用しないだけで、直接インクリメントします。int (*)[5][2]ptrint (*ptr) [5];ptr = *(p + m);ptable_ptrmp

この後、第 3 レベル (最も内側のループ) で、int *x(私のコードではこれはcol_ptr) と言う整数ポインターが必要ですint *x = *(ptr + m1)。基本的に、それぞれが 1 つのレベルに対応する 3 つの異なるポインターが必要です:int (*) [5][2]と。私はそれらにとという名前を付けました。int (*) [2]int *table_ptrrow_ptrcol_ptr

于 2013-10-12T15:10:01.900 に答える
3

以下のコードを書き直し、ポインターを使用して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));
         }         
      }
   }
}
于 2013-10-12T14:18:23.520 に答える
1
#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;

}

2X2X2配列の要素をポインタでアクセスするサンプルコードです。それが役に立てば幸い !!!!

于 2013-10-12T15:18:39.890 に答える
1

2*5*2 = 20ループして配列の最初の要素へのポインタを使用するだけで、すべての要素に簡単にアクセスできます。つまり、 array[0][0][0]3D 配列を 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++));
   }
} 

私はあなたのコードの一部にコメントを付け、変更したコードに残して、私が何をしたかを明確にしました.

于 2013-10-12T13:48:50.537 に答える