gpt4 book ai didi

c - 错误 : subscripted value is neither array nor pointer nor vector

转载 作者:太空宇宙 更新时间:2023-11-04 08:54:52 36 4
gpt4 key购买 nike

我试图在传递二维数组后从函数返回二维数组。这就是我所取得的进展,但我仍然遇到过多的编译错误。我已经尝试搜索错误的含义,但我仍然非常困惑。我正在尝试将一组值旋转 90 度。这是我的代码:

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

这是我与此功能相关的错误:

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]

我还有另一个函数两次调用前一个函数,将数组旋转 180 度,这给了我类似的错误。这是代码:

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

以下是与此函数相关的错误:

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 是一个char *。所以,array[rowCount] 是一个 char。你打算如何进一步下标?

如果您需要一个二维数组,您可能应该将指向数组的指针传递给该函数。

同样,

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

这也是错误的,returnArray2 从函数返回时衰减为 char (*)[columnCount],因此您也应该更改返回类型。

(是的,正如其他人指出的那样,返回一个具有自动存储持续时间的数组会调用 UB,您应该动态分配内存,或者将另一个数组传递给它就地修改的函数。)

关于c - 错误 : subscripted value is neither array nor pointer nor vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17769526/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com