I have created a small piece of code to dynamically allocate a 2D array in C, this is being used to try and solve a problem I am having on a larger piece of code, if I can get my head around dynamically allocating a 2D array I am confident I will solve my other problem. The issue I have had is that after I have allocated the matrix and wrote to it with a function my program does not run, I do not get any errors, it just creates a blank screen and eventually crashes. I am not sure where to progress from here, any help would be much appreciated!
Here is the code:
#include <stdlib.h>
void get_matrix(double **a, int n);
int main() {
int n = 5;
int i, j;
double **a;
a = (double **)malloc(n * sizeof(double *));
for (j = 0; j < n; j++)
a[j] = (double *)malloc(n * sizeof(double));
get_matrix(a, n);
for (i = 0; i <= n; i++) {
for (j = 0; j <= n; j++) {
printf("%d, ", a[i][j]);
}
printf("\n, ");
}
return 0;
}
void get_matrix(double **a, int n) {
int i, j;
for (i = 0; i <= n; i++) {
for (j = 0; j <= n; j++) {
a[i][j] = 4;
}
}
}