使用双指针实现二维数组

请考虑以下代码:

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

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

 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 

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

*(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 

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

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

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

  • ptr指向完成2-D数组,因此ptr+1指向下一个2-D数组。

在下图中, 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

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

了解以下内容:

ptr指向完成2-D。

*ptr = *(ptr + 0) = ptr[0]第一行的*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 

和GDB输出:

 (gdb) p *(*ptr+1) $1 = 2 

这是正确的2这可以使用ptr[0][1]来读取。

  (*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 

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

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

所以ptr正在构建主数组,它将包含指向子数组的指针。 ptr正在向主数组发送,这意味着ptr指向主数组的第一个元素,因此ptr + 1指向主数组的第二个元素。

*ptr这意味着ptr指向的第一个元素的内容。 它是指向子arrays的指针。 所以*ptr是指向第一个子数组的指针(子数组是int数组)。 所以*ptr指向第一个子arrays中的第一个元素。 所以*ptr + 1是指向第一个子arrays中第二个元素的指针

使用指针创建二维数组,分配值和访问数组中元素的最简单方法。

 #include #include int main() { int i,j; int row,col; printf("Enter the values for row and col:\n"); scanf("%d%d",&row,&col); int **arr=(int**)malloc(row*(sizeof(int*))); for(i=0;i 

除非您输入错误,否则(*ptr + 1)等效于*(ptr + 0) + 1 ,它是指向第一个块中第二个元素的指针。