双指针实现二维数组实现

San*_*ngh 8 c double-pointer multidimensional-array

请考虑以下代码:

#include <stdio.h>
#include <stdlib.h>

#define NUM_ARRAYS     4
#define NUM_ELEMENTS   4
#define INVALID_VAL   -1

int main()
{
   int index            = INVALID_VAL;
   int array_index      = INVALID_VAL;
   int **ptr            = NULL;

   ptr = malloc(sizeof(int*)*NUM_ARRAYS);

   if (!ptr)
   {
      printf ("\nMemory Allocation Failure !\n\n");
      exit (EXIT_FAILURE);
   }

   for (index=0; index<NUM_ARRAYS; index++)
   {
      *(ptr+index) = malloc(sizeof(int)*NUM_ELEMENTS); 

      if (!*(ptr+index))
      {
         printf ("\nMemory Allocation Failure !\n");
         exit (EXIT_FAILURE);
      }
   }

   /* Fill Elements Into This 2-D Array */
   for (index=0; index<NUM_ARRAYS; index++)
   {
      for (array_index = 0; array_index<NUM_ELEMENTS; array_index++)
      {
         *(*(ptr+index)+array_index) = (array_index+1)*(index+1);
      }
   }

   /* Print Array Elements */
   for (index = 0; index<NUM_ARRAYS; index++)
   {
      printf ("\nArray %d Elements:\n", index);
      for (array_index = 0; array_index<NUM_ELEMENTS; array_index++)
      {
         printf (" %d ", *(*(ptr+index)+array_index));
      }
      printf ("\n\n");
   }

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的代码没有问题.它工作正常.

Output:

Array 0 Elements:
 1  2  3  4 


Array 1 Elements:
 2  4  6  8 


Array 2 Elements:
 3  6  9  12 


Array 3 Elements:
 4  8  12  16 
Run Code Online (Sandbox Code Playgroud)

我有一个关于指针算术的问题:

*(ptr+0)=指向COMPLETE BLOCK的指针(第一个数组)
*(ptr+1)=指向COMPLETE BLOCK(第二个数组)的指针.

但是:是什么(*ptr+1)

GDB输出:

(gdb) p *(*ptr+1)
$1 = 2
(gdb) p *(*ptr+2)
$2 = 3
(gdb) p *(*ptr+3)
$3 = 4
(gdb) p *(*ptr+4)
$4 = 0
Run Code Online (Sandbox Code Playgroud)

我对此感到困惑.请给我一些解释来解决这个疑问.

MOH*_*MED 26

                                 (*ptr)      (*ptr+1)     (*ptr+2)
                                   |            |            |
             __________      ______v____________v____________v____________
  ptr------>|   *ptr   |--->|  *(*ptr)   |  *(*ptr+1)  |*(*ptr+2) |       |
            |__________|    |____________|_____________|__________|_______|
 (ptr+1)--->| *(ptr+1) |     ____________ _____________ __________________
            |__________|--->|*(*(ptr+1)) |*(*(ptr+1)+1)|          |       |
            |          |    |____________|_____________|__________|_______|
            |__________|          ^             ^
                                  |             |
                              *(ptr+1)     *(ptr+1)+1
Run Code Online (Sandbox Code Playgroud)

具有双指针的2D数组意味着您具有主数组,并且主数组的元素是指向子数组的指针(或地址).如上图所示

所以,如果你已经将双指针定义为这个2D数组的指针,那就说吧 int **ptr

所以ptr是ponting到主数组,它将包含指向子数组的指针.ptrptr指向主数组,它意味着指向主数组的第一个元素,因此ptr + 1指向主数组的第二个元素.

*ptr这意味着ptr指向的第一个元素的内容.它是指向子阵列的指针.所以*ptr是指向第一个子数组的指针(子数组是一个数组int).所以*ptr指向第一个子阵列中的第一个元素.*ptr + 1指向第一个子数组中的第二个元素的指针也是如此

  • 给你的答案+1,你的确切比我的清晰得多.Kallel如何画这个无花果.?你使用任何工具吗? (2认同)

Gri*_*han 9

*(ptr+i)等于ptr[i]*(ptr+1)ptr[1].

你可以认为,二维数组是数组的数组.

  • ptr指向完成二维数组,所以ptr+1指向下一个二维数组.

下图 ptr是2-D和列数3

Kerrek SB先生制作的原始图,在这里,你也应该检查一下!

+===============================+==============================+====
|+---------+----------+--------+|+----------+---------+--------+|
||ptr[0,0] | ptr[0,1] | ptr[0,2]|||ptr[1,0] |ptr[1,1] | ptr[1,2]|| ...
|+---------+----------+--------+++----------+---------+--------++ ...
|            ptr[0]             |           ptr[1]              |
+===============================+===============================+====
   ptr
Run Code Online (Sandbox Code Playgroud)

*(*ptr+1) = *( ptr[0] + 1 ) = ptr[0][1]

了解以下内容:

ptr 指向完成2-D.

*ptr = *(ptr + 0) = ptr[0] 那是第一排.

*ptr + 1 = ptr[1] 意味着第二排

*(*ptr+1) = *(*(ptr + 0) + 1 ) = *(ptr[0] + 1) = ptr[0][1]

Array 0 Elements:
1  2  3  4 
Run Code Online (Sandbox Code Playgroud)

和GDB输出:

(gdb) p *(*ptr+1)
$1 = 2  
Run Code Online (Sandbox Code Playgroud)

这是正确的,2这可以使用ptr[0][1].