8

请考虑以下代码:

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

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

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

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

4

5 回答 5

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

具有双指针的二维数组,这意味着您有一个主数组,主数组的元素是指向子数组的指针(或地址)。如上图所示

所以如果你定义了一个双指针作为这个二维数组的指针,让我们说int **ptr

ptr指向包含指向子数组的指针的主数组也是如此。ptr指向主数组,这意味着ptr指向主数组的第一个元素,因此ptr + 1指向主数组的第二个元素。

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

于 2012-12-20T14:45:51.163 回答
9

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

您可以将二维数组视为数组数组。

  • ptr指向完整的二维数组,所以ptr+1指向下一个二维数组。

下图中 ptr是二维的,列数是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点完成二维。

*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].

于 2012-12-20T14:18:35.050 回答
3

使用指针创建二维数组、赋值和访问数组元素的最简单方法。

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

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<row;i++)
{
    *(arr+i)=(int*)malloc(sizeof(int)*col);
            //You can use this also. Meaning of both is same.
            //arr[i]=(int*)malloc(sizeof(int)*col);
}
for(i=0;i<row;i++)
for(j=0;j<col;j++)
{
    arr[i][j]=0;
}
for(i=0;i<row;i++)
{
    for(j=0;j<col;j++)
    {
        printf("%d ",arr[i][j]);
    }
    printf("\n");
}
}
于 2013-10-17T16:22:00.973 回答
2

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

于 2012-12-20T14:15:06.553 回答
1

*ptr + 1是地址ptr[0][1]

因为,我们知道

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

现在把&符号放在两边

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

你知道带星号的和号就像加号和减号一样吗?他们被取消。所以这变成

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

这与

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

这又是一样的

&ptr[0][1] == *ptr+1

这是我的第一个声明。

于 2020-08-05T19:24:12.207 回答