C语言 分配数组元素值时,C 下标值既不是数组也不是指针也不是向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19908456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C subscripted value is neither array nor pointer nor vector when assigning an array element value
提问by Euphe
Sorry for asking the already answered question, I am a newbie to C and don't understand the solutions. Here is my function
很抱歉提出已经回答的问题,我是 C 的新手,不了解解决方案。这是我的功能
int rotateArr(int *arr) {
int D[4][4];
int i = 0, n =0;
for(i; i < M; i ++ ){
for(n; n < N; n++){
D[i][n] = arr[n][M - i + 1];
}
}
return D;
}
It throws an error
它抛出一个错误
main.c|23|error: subscripted value is neither array nor pointer nor vector|
main.c|23|错误:下标值既不是数组也不是指针也不是向量|
on line
在线的
D[i][n] = arr[n][M - i + 1];
D[i][n] = arr[n][M - i + 1];
What's wrong? I am just setting the value of an array element to another array element.
怎么了?我只是将一个数组元素的值设置为另一个数组元素。
The arr passed is declared as
传递的 arr 声明为
int S[4][4] = { { 1, 4, 10, 3 }, { 0, 6, 3, 8 }, { 7, 10 ,8, 5 }, { 9, 5, 11, 2} };
回答by dasblinkenlight
C lets you use the subscript operator []on arrays and on pointers. When you use this operator on a pointer, the resultant type is the type to which the pointer points to. For example, if you apply []to int*, the result would be an int.
C 允许您[]在数组和指针上使用下标运算符。在指针上使用此运算符时,结果类型是指针指向的类型。例如,如果你申请[]到int*,结果将是一个int。
That is precisely what's going on: you are passing int*, which corresponds to a vector of integers. Using subscript on it once makes it int, so you cannot apply the second subscript to it.
这正是正在发生的事情:您正在传递int*,它对应于一个整数向量。对它使用下标一次就可以了int,所以你不能对它应用第二个下标。
It appears from your code that arrshould be a 2-D array. If it is implemented as a "jagged" array (i.e. an array of pointers) then the parameter type should be int **.
从您的代码看来,它arr应该是一个二维数组。如果它被实现为“锯齿状”数组(即指针数组),则参数类型应该是int **.
Moreover, it appears that you are trying to return a local array. In order to do that legally, you need to allocate the array dynamically, and return a pointer. However, a better approach would be declaring a special structfor your 4x4 matrix, and using it to wrap your fixed-size array, like this:
此外,您似乎正在尝试返回本地数组。为了合法地做到这一点,您需要动态分配数组,并返回一个指针。但是,更好的方法是struct为 4x4 矩阵声明一个特殊矩阵,并使用它来包装固定大小的数组,如下所示:
// This type wraps your 4x4 matrix
typedef struct {
int arr[4][4];
} FourByFour;
// Now rotate(m) can use FourByFour as a type
FourByFour rotate(FourByFour m) {
FourByFour D;
for(int i = 0; i < 4; i ++ ){
for(int n = 0; n < 4; n++){
D.arr[i][n] = m.arr[n][3 - i];
}
}
return D;
}
// Here is a demo of your rotate(m) in action:
int main(void) {
FourByFour S = {.arr = {
{ 1, 4, 10, 3 },
{ 0, 6, 3, 8 },
{ 7, 10 ,8, 5 },
{ 9, 5, 11, 2}
} };
FourByFour r = rotate(S);
for(int i=0; i < 4; i ++ ){
for(int n=0; n < 4; n++){
printf("%d ", r.arr[i][n]);
}
printf("\n");
}
return 0;
}
This prints the following:
这将打印以下内容:
3 8 5 2
10 3 8 11
4 6 10 5
1 0 7 9
回答by Sadique
You are not passing your 2D array correctly. This should work for you
您没有正确传递二维数组。这应该适合你
int rotateArr(int *arr[])
or
或者
int rotateArr(int **arr)
or
或者
int rotateArr(int arr[][N])
Rather than returning the array pass the target array as argument. See John Bode's answer.
而不是返回数组,而是将目标数组作为参数传递。请参阅约翰博德的回答。
回答by John Bode
Except when it is the operand of the sizeofor unary &operator, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T" is converted ("decays") to an expression of type "pointer to T", and the value of the expression is the address of the first element of the array.
除非它是sizeof或 一元运算&符的操作数,或者是用于在声明中初始化另一个数组的字符串文字,否则“N 元素数组T”类型的表达式将转换(“衰减”)为类型的表达式“指向”的指针T,表达式的值是数组第一个元素的地址。
If the declaration of the array being passed is
如果传递的数组的声明是
int S[4][4] = {...};
then when you write
那么当你写
rotateArr( S );
the expression Shas type "4-element array of 4-element array of int"; since Sis not the operand of the sizeofor unary &operators, it will be converted to an expression of type "pointer to 4-element array of int", or int (*)[4], and this pointervalue is what actually gets passed to rotateArr. So your function prototype needs to be one of the following:
表达式的S类型为“4 元素数组的 4 元素数组int”;由于S不是sizeof或 一元运算&符的操作数,它将被转换为“指向 4 元素数组的指针”类型的表达式int,或int (*)[4],并且该指针值是实际传递给 的rotateArr。所以你的函数原型需要是以下之一:
T rotateArr( int (*arr)[4] )
or
或者
T rotateArr( int arr[][4] )
or even
甚至
T rotateArr( int arr[4][4] )
In the context of a function parameter list, declarations of the form T a[N]and T a[]are interpreted as T *a; all three declare aas a pointer to T.
在函数参数列表的上下文中,形式的声明T a[N]和T a[]被解释为T *a; 所有三个都声明a为指向T.
You're probably wondering why I changed the return type from intto T. As written, you're trying to return a value of type "4-element array of 4-element array of int"; unfortunately, you can't do that. C functions cannotreturn array types, nor can you assign array types. IOW, you can't write something like:
您可能想知道为什么我将返回类型从 更改int为T。正如所写的,您试图返回一个类型为“4-element array of 4-element array of int”的值;不幸的是,你不能那样做。C 函数不能返回数组类型,也不能分配数组类型。IOW,你不能写这样的东西:
int a[N], b[N];
...
b = a; // not allowed
a = f(); // not allowed either
Functions can return pointersto arrays, but that's not what you want here. Dwill cease to exist once the function returns, so any pointer you return will be invalid.
函数可以返回指向数组的指针,但这不是您在这里想要的。 D函数返回后将不复存在,因此您返回的任何指针都将无效。
If you want to assign the results of the rotated array to a different array, then you'll have to pass the target array as a parameter to the function:
如果要将旋转数组的结果分配给不同的数组,则必须将目标数组作为参数传递给函数:
void rotateArr( int (*dst)[4], int (*src)[4] )
{
...
dst[i][n] = src[n][M - i + 1];
...
}
And call it as
并将其称为
int S[4][4] = {...};
int D[4][4];
rotateArr( D, S );
回答by NPE
The problem is that arris not (declared as) a 2D array, and you are treating it as if it were 2D.
问题在于它arr不是(声明为)二维数组,您将其视为二维数组。
回答by Peter Cardona
You have "int* arr" so "arr[n]" is an int, right? Then your "[M - 1 + 1]" bit is trying to use that int as an array/pointer/vector.
你有“int* arr”所以“arr[n]”是一个int,对吧?然后您的“[M - 1 + 1]”位试图将该 int 用作数组/指针/向量。
回答by nathan
the second subscript operator is invalid here. You passed a int * pointer into function, which is a 1-d array. So only one subscript operator can be used on it.
第二个下标运算符在这里无效。您将 int * 指针传递给函数,这是一个一维数组。因此只能在其上使用一个下标运算符。
Solution : you can pass int ** pointer into funciton
解决方案:您可以将 int ** 指针传递给函数

