C indexed value is neither an array, nor a pointer, nor a vector when assigning a value to an array element

Sorry to ask the already answered question, I am new to C and don’t understand the solution. Here is my function

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

He gives an error

main.c | 23 | error: the indexed value is neither an array, nor a pointer and vector

in line

D [i] [n] = arr [n] [M - i + 1];

What happened? I just set the value of an array element to another array element.

The accepted arr is declared as

 int S[4][4] = { { 1, 4, 10, 3 }, { 0, 6, 3, 8 }, { 7, 10 ,8, 5 }, { 9, 5, 11, 2} }; 
+7
c arrays
source share
6 answers

C allows you to use the index operator [] for arrays and pointers. When you use this operator on a pointer, the resulting type is the type that the pointer points to. For example, if you apply [] to int* , the result is int .

This is exactly what happens: you pass int* , which corresponds to a vector of integers. Using an index on it once makes it int , so you cannot apply a second index to it.

From your code you can see that arr should be a two-dimensional array. If it is implemented as a jagged array (that is, an array of pointers), then the type of the parameter must be int ** .

Also, it looks like you are trying to return a local array. To do this legally, you need to distribute the array dynamically and return a pointer. However, a better approach would be to declare a special struct for your 4x4 matrix and use it to wrap your array of a fixed size, for example:

 // 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 files :

 3 8 5 2 10 3 8 11 4 6 10 5 1 0 7 9 
+11
source share

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]) 

Instead of returning an array, pass the target array as an argument. See John Bode's Answer.

+4
source share

sizeof it is an operand of the sizeof or unary & operator or is a string literal used to initialize another array in the declaration, an expression of the type "N-element array of T " is converted ("decays") to an expression of the type "pointer to T " , and the value of the expression is the address of the first element of the array.

If an array declaration is passed

 int S[4][4] = {...}; 

then when you write

 rotateArr( S ); 

expression S is of type "4-element array of a 4-element array int "; since S not an operand of the sizeof or unary & operators, it will be converted to an expression like β€œpointer to a 4-element array int ” or int (*)[4] , and this pointer value is what is actually passed to rotateArr . So your function prototype should be one of the following:

 T rotateArr( int (*arr)[4] ) 

or

 T rotateArr( int arr[][4] ) 

or even

 T rotateArr( int arr[4][4] ) 

In the context of the parameter list, the declaration functions of the form T a[N] and T a[] interpreted as T *a ; all three declare a as a pointer to T

You are probably wondering why I changed the return type from int to T As written, you are trying to return a value like "4-element array from a 4-element int array"; Unfortunately, you cannot do this. C functions cannot return array types, nor can they assign array types. IOW, you cannot write something like:

 int a[N], b[N]; ... b = a; // not allowed a = f(); // not allowed either 

Functions can return pointers to arrays, but that is not what you want here. D will cease to exist after the function returns, so any returned pointer will be invalid.

If you want to assign the results of a rotated array to another array, you need 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 him

 int S[4][4] = {...}; int D[4][4]; rotateArr( D, S ); 
+4
source share

The problem is that arr not (declared as) a 2D array, and you treat it as if it were 2D.

+1
source share

You have "int * arr", so "arr [n]" is int, right? Then your bit [M - 1 + 1] tries to use this int as an array / pointer / vector.

0
source share

the second index operator is not valid here. You passed int * a pointer to a function, which is the 1st array. Therefore, you can use only one subscript operator on it.

Solution: you can pass int ** pointer to funciton

0
source share

All Articles