简体   繁体   中英

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. 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. 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. 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 * . So, array[rowCount] is a 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.

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.

(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.)

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.

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.

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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