Translation of a column from a matrix into a function from c

I want to pass a specific matrix column, which I have in my program, to a function. If I use the call <function_name>(<matrix_name>[][<index>]);as a call, then I get an error

error: expected expression before '] Marker

So please help me find a suitable way Thanks

+3
source share
4 answers

The syntax you used does not exist.

Matrices are stored in memory by row (or better, by the second dimension, to which you give the semantics of rows), so you cannot initially. You can copy all the elements of a column into a vector (one dimensional array) and transfer it.

( ), , : matrix[row][column] matrix[column][row].

, , .

+5

. . , , :

arr[5][4]

"" . .

+1

, C .

, 2D-, :

float mat[4][4];

, , 16 , ; , [3] [2], - , . , - . [3] [2] :

mat[ (3*4 + 2) ]

, , , :

void do_processing(float* mat, int columns, int rows, int column_idx)

, :

mat[ (column_idx * rows) + row_idx ]

+1

Due to how addressing works, you cannot just pass in a โ€œcolumnโ€, since the values โ€‹โ€‹of the โ€œcolumnโ€ are actually stored in your โ€œrowsโ€. This is why your compiler will not allow you to pass the value in your link to "string", that is: "[]".

A simple solution would be to pass the entire matrix and pass the column index as a separate integer and number of rows. Your function can then iterate over each row to access all members of this column. i.e:

functionName(matrixType** matrixRef, int colIndex, int numRows)  
{  
   for(int i=0; i< numRows; ++i)  
      matrixType value = matrixRef[i][colIndex]; //Do something  
}
+1
source

Source: https://habr.com/ru/post/1789296/


All Articles