I study declaring two-dimensional dynamic arrays in C. I assumed that dynamic arrays here can be declared only with the help of references and malloc ().
At first I used malloc () in a loop.
int m, n, c; scanf("%d %d", &m, &n); int** a = (int**)malloc(m*sizeof(int*)); for (c = 0; c < m; c++) { a[c] = (int*)malloc(n*sizeof(int)); } Then came the only call to malloc ().
int m, n, c; scanf("%d %d", &m, &n); int** a = (int**)malloc(m*sizeof(int*) + m*n*sizeof(int)); int* row = &a[n]; for (c = 0; c < m; row += n, c++) { a[c] = row; } And then I tried the elementary:
int m, n; scanf("%d %d", &m, &n); int a[m][n]; And it worked. This is normal? Can this be used?