I am trying to represent a matrix as an array of pointers, and then do certain actions on the matrix.
To simplify further, imagine a matrix with n
rows and m
columns,
I defined an int **matrix
that points to the beginning of an array (of pointers) of size n
. And every block in this array is a pointer that points to an array (of integers) of size m
. The result is clear. you have n
pointers, each of them pointing to an array of m
integers, overall nxm
values. So it should be possible to represent a matrix this way.
My problem is that I don't know how to access the integer values of the matrix. For example, suppose I wish to insert values to the matrix. How do I access matrix[i][j]
?
Here is my code, you can see it is incomplete, I would appreciate help completing it.
int** create_matrix(int rows,int columns)
{
int** matrix,i;
matrix=(int**)malloc(rows*sizeof(int*));
for(i=0;i<rows;i++)
*(matrix)+i=(int*)malloc(columns*sizeof(int));
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
*(*(matrix)+i)
I want to insert random numbers into the matrix and then return it. I allocated the necesary memory, just need to insert the actual values.