Ciao a tutti,
ho un problema con l'assegazione dei valori ad una matrice creata dinamicamente. Appena provo ad essegnare i valori, mi dà errore di segmentazione.
codice:
void matrix_allocation(int **matrix, int N){
int i, j;
/* dynamic allocation for the matrix */
/*allocation of the rows*/
matrix = (int**)malloc(N * sizeof(int*));
if(matrix == NULL){
printf("\n Error in memory allocation for the matrix \n");
}
/*allocation of the coloumns*/
for(i=0; i<N; i++){
matrix[i]=(int*) malloc(N * sizeof(int));
if (matrix[i] == NULL){
printf("Cannot allocate enough memory\n");
}
}
}
void generate_matrix(int **matrix, int N){
int i, j;
for (i=0; i<N; i++){
for (j=0; j<N; j++){
if(i==j){ //the diagonal must be 0
matrix[i][j] = 0;
}
else{
matrix[i][j] = rand()%10;
printf("%i\t", matrix[i][j]);
}
}
printf("\n");
}
printf("\nMatrix generated successfully!\n");
}
Appena arriva a matrix[i][j] = 0 (alla prima esecuzione), dà errore. Cosa sbaglio? Avete qualche idea? Grazie a tutti.