I am this method to initialize 2D array:
// begin code
int **a, int r, int c;
int i;
int **a = (int *) malloc (r * sizeof (int *));
for (i=0;i<c;i++)
*a = (int *) malloc(c * sizeof(int));
// end code
if I extend this to 3-D array, I got error:
// begin code
int ***a, int n, int r, int c;
int i, j;
int *** = (int **) malloc (n * sizeof (int **));
for (i = 0; i < n; i++) {
int **a = (int *) malloc (r * sizeof(int *));
for (j = 0; j < r; j++) {
*a[j] = (int *) malloc (c * sizeof(int));
}
}
// end code
What is wrong? What is the correct way?
// begin code
int **a, int r, int c;
int i;
int **a = (int *) malloc (r * sizeof (int *));
for (i=0;i<c;i++)
*a = (int *) malloc(c * sizeof(int));
// end code
if I extend this to 3-D array, I got error:
// begin code
int ***a, int n, int r, int c;
int i, j;
int *** = (int **) malloc (n * sizeof (int **));
for (i = 0; i < n; i++) {
int **a = (int *) malloc (r * sizeof(int *));
for (j = 0; j < r; j++) {
*a[j] = (int *) malloc (c * sizeof(int));
}
}
// end code
What is wrong? What is the correct way?