So I have the following function. The function takes a target int ** double pointer and copies a source array to it.
void copyMatrix(int ** target, int m, int n, int source[m][n])
{
int i,j;
target = (int **) malloc(m * sizeof(int *));
for (i = 0; i < m; i++)
target[i] = (int *)malloc(n * sizeof(int));
for(i=0; i < m; i++){
for(j=0; j < n; j++){
target[i][j] = source[i][j];
}
}
printf("%d ", target[i][j]);
}
When i call printf after the for loops, i get a segfault, but if i call printf inside the for loops, it prints target[i][j] correctly. Why is this? I'm tearing my hair out over this...