C 获取2D数组的错误:下标值既不是数组,也不是指针或向量

C 获取2D数组的错误:下标值既不是数组,也不是指针或向量,c,arrays,multidimensional-array,spiral,C,Arrays,Multidimensional Array,Spiral,嗨,我正在写一个以螺旋顺序打印2D数组的程序。但是我得到下面的错误下标值既不是数组,也不是指针,也不是向量 这是我的密码 int *spiralOrder(const int** A, int n11, int n12, int *length_of_array) { *length_of_array = n11 * n12; // length of result array int *result = (int *)malloc(*length_of_array * size

嗨,我正在写一个以螺旋顺序打印2D数组的程序。但是我得到下面的错误
下标值既不是数组,也不是指针,也不是向量

这是我的密码

int *spiralOrder(const int** A, int n11, int n12, int *length_of_array) {
    *length_of_array = n11 * n12; // length of result array
    int *result = (int *)malloc(*length_of_array * sizeof(int));
    int top = 0, bottom = n11 - 1, left = 0, right = n12 - 1;
    int d = 0, j = 0, k = 0;
    while (top <= bottom - 1 && left <= right - 1) {
        int i;
        if (d == 0) { //left to right
            for (i = 0; i < right; i++) {
                result[j++][k++] = A[top][i];
            }
            top++;
            d = 1;
        } else
        if (d == 1) { //top to bottom
            for (i = 0; i < bottom; i++) {
                result[j++][k++] = A[i][right];
            }
            right--;
            d = 2;
        } else
        if (d == 2) { //bottom right to left
            for (i = right; i >= left; i--) {
                result[j++][k++] = A[bottom][i];
            }
            bottom--;
            d = 3;
        } else
        if (d == 3) {  //bottom left to top
            for (i = bottom; i >= top; i--) {
                result[j++][k++] = A[i][left];
            }
            left++;
            d = 0;
        }
    }
    return result;
}
int*spiralOrder(常量int**A,int n11,int n12,int*length\u数组){
*数组的长度=n11*n12;//结果数组的长度
int*result=(int*)malloc(*数组的长度*sizeof(int));
int top=0,bottom=n11-1,left=0,right=n12-1;
int d=0,j=0,k=0;
而(top=top;i--){
结果[j++][k++]=A[i][左];
}
左++;
d=0;
}
}
返回结果;
}

我想将结果保存在
result
数组中。我看到了这些错误的答案,但大多数都是处理一维数组。有人能帮忙吗。

您将
malloc
ed
result
作为一维数组,并将其用作双对数数组

假设数组大小有两个变量
row\u number
col\u number
,您应该像这样分配
result
(不要忘记
free()
):

int**result;
结果=(int**)malloc(行号*sizeof(int*);

对于(int i=0;i首先,您将结果创建为int*但将其用作int**。
第二,如果不打算更改2D数组,请使用int**const而不是const int**。
最后,您没有正确实现逻辑,
等待,只有在无法实现逻辑时才进一步读取。以下是正确的实现:

int*spiralOrder(int**const A,int n11,int n12,int*length_数组){
*数组的长度=n11*n12;//结果数组的长度
int*result=(int*)malloc(*数组的长度*sizeof(int));
int top=0,bottom=n11-1,left=0,right=n12-1;
int j=0,k=0;

while(top
result
是一个指针。你可以像一维数组一样对它进行索引。但是你像二维数组一样对它进行索引?你能帮我这样做吗?
int **result;
result = (int**) malloc(row_number * sizeof(int*));
for(int i=0; i<row_number; i++)
{
    result[i] = (int*) malloc(col_number * sizeof(int));
}
int *spiralOrder(int** const A, int n11, int n12, int *length_of_array) {
    *length_of_array = n11 * n12; // length of result array
    int *result = (int *)malloc(*length_of_array * sizeof(int));
    int top = 0, bottom = n11 - 1, left = 0, right = n12 - 1;
    int j = 0, k = 0;
    while (top <= bottom - 1 || left <= right - 1) {
        int i;
         //left to right
            for (i = left; i <= right; i++) {
                result[j++] = A[top][i];
            }
            top++;
            //top to bottom
            for (i = top; i <= bottom; i++) {
                result[j++] = A[i][right];
            }
            right--;
            //bottom right to left
            for (i = right; i >= left; i--) {
                result[j++] = A[bottom][i];
            }
            bottom--;
              //bottom left to top
            for (i = bottom; i >= top; i--) {
                result[j++] = A[i][left];
            }
            left++;
    }
    return result;
}