pointers - allocate matrix in C -
I want to allot the matrix.
This is the only option:
int ** mat = (int **) Malloc (rows * size (integer *)) {mat [index] = (int *) malloc (cola * size); }
Well, you did not give us a complete implementation. I believe that your it means.
int ** mat = (int **) malloc (rows * size (int *)); For (int i = 0; i & lt; rows; i ++) mat [i] = (int *) maulok (column * sizaf (int));
Here is another option:
int * mat = (int *) malloc (rows * cols * sizeof (int));
Then, you
int offset = i * cols + j; Using the matrix. // Now Mat [offset] matches (i, j)
for mail and
int offset = i + rows * j; // no mat [offset] corresponds to M (i, j)
.
One of these two options is actually the preferred way to handle a matrix in C. This is because now the matrix will be stored in memory and you will get the benefit. In fact, the CPU cache will be very happy with you.
Comments
Post a Comment