1
I know how to create a matrix using dynamic allocation only. However, I’m not able to create using structures.
I have the following struct:
struct matriz{
int** elemento;
int nlinhas;
int ncolunas;
};
and I used typedef to make it easier:
typedef struct matriz Matriz;
So I tried to implement the following function:
Matriz* inicializaMatriz(int nlinhas, int ncolunas){
Matriz* mat;
mat = (Matriz*)malloc(sizeof(int));
mat->nlinhas = (int*)malloc(sizeof(int) * nlinhas);
mat->ncolunas = (int*)malloc(sizeof(int) * ncolunas);
return mat;
}
However, when I try to implement this other function that will assign values to each element of the matrix:
void modificaElemento(Matriz* mat, int linha, int coluna, int elem){
mat[linha][coluna]->elemento = elem;
}
I get the message that it is not possible(subscrited value is neither array nor Pointer nor vector)
With this, I would like help to create a matrix using the structure already mentioned.
If in your Matrix struct
nlinhas
andncolunas
are int fields so the assignments:mat->nlinhas = (int*)malloc(sizeof(int) * nlinhas);
andmat->ncolunas = (int*)malloc(sizeof(int) * ncolunas);
make no sense.– anonimo
but I don’t have to allocate memory for rows and columns?
– Enzo Nunes