简体   繁体   English

C下标值既不是数组也不是指针也不是向量

[英]C subscripted value is neither array nor pointer nor vector

int * matrixsum(int *a,int *b,int n,int m)
{
    int *p=NULL,i,j;
    p=malloc(sizeof(int)*n*m);
    if(p==NULL)
    {
        printf("Error!\n");
        exit(1);
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            *(p+i*n+j)=*(a+i*n+j)+*(b+i*n+j);
        }
    }
    return p;
}

My question is about the line *(p+i*n+j)=*(a+i*n+j)+*(b+i*n+j); 我的问题是关于行*(p+i*n+j)=*(a+i*n+j)+*(b+i*n+j); : if I replace it with p[i][j]=a[i][j]+b[i][j]; :如果我将其替换为p[i][j]=a[i][j]+b[i][j]; I get the following error 3 times: 我收到以下错误3次:

error: subscripted value is neither array nor pointer nor vector 错误:下标值既不是数组也不是指针也不是向量

Why? 为什么? From my knowledge they are the same thing. 据我所知,它们是同一回事。

My compiler is gcc version 4.6.3. 我的编译器是gcc版本4.6.3。

They're not the same thing at all — which is why the compiler is complaining! 它们根本不是一回事,这就是编译器抱怨的原因! You could write: 你可以写:

p[i*n+j] = a[i*n+j] + b[i*n+j];

The type of p is int * ; p的类型为int * ; therefore the type of p[i] is int , and you can't subscript an int . 因此p[i]的类型是int ,并且不能下标int You'd have to be passing a 2D-array of int , or an array of pointers to int , to be able to use the p[i][j] notation. 您必须传递一个int的2D数组或一个指向int的指针数组,才能使用p[i][j]表示法。 For example, in C99 (using variable-length arrays — and note the reordering of the parameters): 例如,在C99中(使用可变长度数组-并注意参数的重新排序):

int *matrixsum(int n, int m, int a[m][n], int b[m][n])
{
    ...
    p[i][j] = a[i][j] + b[i][j];
    ...
}

Or, with some considerable care in the setup, you could use: 或者,在设置时要格外小心,您可以使用:

int *matrixsum(int **a, int **b, int m, int n)
{
    ...
    p[i][j] = a[i][j] + b[i][j];
    ...
}

Note that for this latter example, you can't simply write: 请注意,对于后一个示例,您不能简单地编写:

int a[4][4] = { ... };
int b[4][4] = { ... };
int r = matrixsum(a, b, 4, 4);

The memory allocation for the 2D array is quite different from what is required for the int ** notation. 2D数组的内存分配与int **表示法所需的内存分配有很大不同。

The compiler is simply telling you that you can't de-reference an integer. 编译器只是告诉您不能取消引用整数。 p is only an int pointer - similarly for a , b , and c . p只是一个int指针-与abc相似。 You can simulate p[i][j] by doing the tricky pointer arithmetic into your buffer, but you can't de-reference an int . 您可以通过对缓冲区执行棘手的指针算法来模拟 p[i][j] ,但不能取消引用int

These variables must be int ** before you can use them as a 2D array, using array subscripts. 在使用数组下标将它们用作2D数组之前,这些变量必须为int **

from my knowledge they are the same thing 据我所知,他们是同一回事

Yes, you've converted from linear indexing to subscripts correctly, but all 3 of those variables are of the wrong type to apply those subscripts. 是的,您已正确地从线性索引转换为下标,但是所有这三个变量的类型都不正确,无法应用这些下标。 They need to be of type int ** for you to be able to do 2-D array indexing. 它们必须是int **类型,才能进行二维数组索引。

In the second form, how can the compiler guess that n is the length of the first dimension of YOUR tab (not his one) ? 在第二种形式中,编译器如何猜测n是YOUR选项卡(不是他的)的第一维的长度? to compute &t[i][j] == *(t + i * n + j) 计算&t [i] [j] == *(t + i * n + j)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM