I'm writing a function that allocates storage to an nxn matrix.
void assign_matrix_storage(double **matrix, int n){
if((matrix = malloc(n * sizeof(double*))) == NULL){
printf("ERROR: Memory allocation failed\n");
exit(EXIT_FAILURE);
}
int i;
for(i = 0; i < n; i++){
if((matrix[i] = malloc(n * sizeof(double))) == NULL){
printf("ERROR: Memory allocation failed\n");
exit(EXIT_FAILURE);
}
}
return;
}
However if I run the following code, I get a segfault on the last statement:
double **A;
assign_matrix_storage(A, 2);
A[1][1] = 42;
Why is this?