George K
George K

Reputation:

Sending the contents of a matrix in one function to a matrix in another function

How can I pass the contents of a char matrix in one function to another matrix of equal dimensions in another function?

Upvotes: 1

Views: 760

Answers (3)

Tom
Tom

Reputation: 45174

You can copy the matrix contents the memcpy function.

/*
* mat: matrix to duplicate.
* rows: rows in mat.
* cols: cols in mat.
*/
void matdup(char mat[][],size_t rows,size_t cols){

    char dupmat[rows][cols];//duplicate matrix in stack.

    memcpy((void*)dupmat,(void*)mat,rows*cols);

    //do stuff with matrix here.
}

With this, you can have a new matrix to use inside of the matdup function.

Upvotes: 3

Noldorin
Noldorin

Reputation: 147461

Since you appear to know that the matrices are of constant size, the task is as simple as passing a double pointer (char** here) to the function, which refers to the matrix being passed.

You will need to pass the dimensions if the rows/columns of the passed matrix is unknown, however, since C does not track the size of arrays in memory (they are no more than pointers with syntactic sugar).

Upvotes: 2

quant_dev
quant_dev

Reputation: 6231

Pass the pointer to the first element in the matrix, and the size.

Upvotes: 1

Related Questions