简体   繁体   English

错误:下标值既不是数组也不是指针也不是向量

[英]Error: subscripted value is neither array nor pointer nor vector

I'm trying to return a 2D array from a function after passing a 2D array. 我正在尝试在传递2D数组后从函数返回2D数组。 This is how far I've gotten, but I'm still getting a plethora of compilation errors. 这是我已经走了多远,但我仍然得到了大量的编译错误。 I've tried searching what the error means however I'm still incredibly confused. 我已经尝试过搜索错误意味着什么但是我仍然非常困惑。 I'm attempting to rotate an array of values by 90 degrees. 我正在尝试将值数组旋转90度。 There is my code: 有我的代码:

// Rotate Array 90 degrees
char * Rotate90Array(char *array, int rowCount, int columnCount) {
  // These have to be swapped because the image is being rotated
  char *returnArray[columnCount][rowCount];
  int u = rowCount - 1;
  int v = columnCount - 1;
  int i = 0;
  int j = 0;
  for (i = 0; i < rowCount; i++) {
    for (j = 0; j < columnCount; j++) {
      returnArray[i][j] = array[u-j][i];
      j++;
    }
    i++;
  }
  return returnArray;
}

Here are my errors relevant to this function: 以下是与此功能相关的错误:

P-MFunctionHolder.c: In function 'Rotate90Array':
P-MFunctionHolder.c:211:34: error: subscripted value is neither array nor pointer nor vector
P-MFunctionHolder.c:216:2: warning: return from incompatible pointer type [enabled by default]
P-MFunctionHolder.c:216:2: warning: function returns address of local variable [enabled by default]

I also have another function that calls upon the previous one twice, to rotate the array 180 degrees, and this is giving me similar errors. 我还有另一个函数调用前一个函数两次,将数组旋转180度,这给了我类似的错误。 Here is the code: 这是代码:

// Rotate Array 180 degrees
char * Rotate180Array(char *array, int rowCount, int columnCount) {
  char returnArray1[rowCount][columnCount] = Rotate90Array(array, rowCount, columnCount);
  char returnArray2[rowCount][columnCount] = Rotate90Array(returnArray1, rowCount, columnCount);
  return returnArray2;
}

Here are the errors relevant to this function: 以下是与此功能相关的错误:

P-MFunctionHolder.c: In function 'Rotate180Array':
P-MFunctionHolder.c:222:2: error: variable-sized object may not be initialized
P-MFunctionHolder.c:223:2: error: variable-sized object may not be initialized
P-MFunctionHolder.c:223:2: warning: passing argument 1 of 'Rotate90Array' from incompatible pointer type [enabled by default]
P-MFunctionHolder.c:199:8: note: expected 'char *' but argument is of type 'char (*)[(sizetype)(columnCount)]'
P-MFunctionHolder.c:224:2: warning: return from incompatible pointer type [enabled by default]
P-MFunctionHolder.c:224:2: warning: function returns address of local variable [enabled by default]

array is a char * . array是一个char * So, array[rowCount] is a char . 所以, array[rowCount]是一个char How do you intend to subscript that further? 你打算如何进一步下标?

If you need a 2D array, you should probably pass a pointer-to-array to the function. 如果需要2D数组,则应该将指针指向数组传递给函数。

Likewise, 同样,

char returnArray2[rowCount][columnCount] = ...
return returnArray2;

This is wrong too, again, returnArray2 decays into char (*)[columnCount] when returned from a function, so you should change the return type as well. 这也是错误的,当从函数返回时, returnArray2会衰减为char (*)[columnCount] ,因此您也应该更改返回类型。

(And yes, as others pointed out, returning an array with automatic storage duration invokes UB, you should either allocate memory dynamically instead or pass another array to the function which it modifies in-place.) (是的,正如其他人指出的那样,返回一个具有自动存储持续时间的数组调用UB,你应该动态地分配内存,或者将另一个数组传递给它就地修改的函数。)

First of all, you CANNOT return arrays by value in C. You can pass them as parameters, of course, but just remember that with arrays of more than one dimension you have to specify the sizes of all but the first dimension. 首先,您不能通过C中的值返回数组。当然,您可以将它们作为参数传递,但请记住,对于包含多个维度的数组,您必须指定除第一个维度之外的所有维度的大小。

You can, however, allocate multidimensional arrays (ie: arrays of pointers to arrays) on the heap which can be returned from functions and passed as "simple" parameters (a dynamic 2d array would be passed as a char** / char*[]), BUT you must be very careful to delegate ownership of the memory properly, otherwise memory leaks await. 但是,您可以在堆上分配多维数组(即:指向数组的指针数组),这些数组可以从函数返回并作为“简单”参数传递(动态2d数组将作为char ** / char *传递[ ]),但你必须非常小心地正确委派内存的所有权,否则等待内存泄漏。

My personal preference would be to use a structure to encapsulate the latter along with the dimension information. 我个人的偏好是使用一种结构来封装后者以及尺寸信息。

Oh, one more thing: 哦,还有一件事:

char *returnArray[columnCount][rowCount];

The above is a 2d array of pointers , which is NOT what you want here... 以上是一个2d 指针数组,这不是你想要的...

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

相关问题 “错误:下标值既不是数组,也不是指针,也不是矢量” - “error: subscripted value is neither array nor pointer nor vector” 持续错误:下标值既不是数组也不是指针也不是向量 - persistent error: subscripted value is neither array nor pointer nor vector 错误:C中的下标值既不是数组也不是指针也不是矢量 - error: subscripted value is neither array nor pointer nor vector in C 错误-下标值既不是数组也不是指针也不是向量 - error - subscripted value is neither array nor pointer nor vector 下标值既不是数组也不是指针也不是向量的错误 - Error with subscripted value being neither array nor pointer nor vector 下标值的错误既不是数组也不是指针也不是向量 - Error on subscripted value is neither array nor pointer nor vector “错误:下标值既不是数组,也不是指针,也不是矢量”,但是为什么呢? - “error: subscripted value is neither array nor pointer nor vector”, but why? 错误 - C 中的下标值既不是数组也不是指针也不是向量 - Error - Subscripted value is neither array nor pointer nor vector in C 下标值既不是数组也不是指针,也不是数组索引处的向量 - Subscripted value is neither array nor pointer nor vector at array index 下标的值既不是数组,也不是指针,也不是向量 - subscripted value is neither array nor pointer nor vector in c
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM