/* * Program to demonstrate 2D array using malloc, two ways. */ #include #include #include void printarray(int rows, int cols, int **nums) { for (int r = 0; r < rows; ++r) { for (int c = 0; c < cols; ++c) { printf("%10d ", nums[r][c]); } putchar('\n'); } } void fill(int rows, int cols, int **nums) { for (int r = 0; r < rows; ++r) { for (int c = 0; c < cols; ++c) { nums[r][c] = 1000 * r + c; } } } int main(void) { printf("enter rows, cols\n"); int rows, cols; if (scanf("%d %d", &rows, &cols) != 2) { printf("error\n"); return 1; } /* * one way to make a 2D array: * allocate each row separately */ /* array of pointers to rows */ int **row_ptrs1 = malloc(sizeof(row_ptrs1[0]) * rows); /* fill array of pointers to rows */ for (int r = 0; r < rows; ++r) { row_ptrs1[r] = malloc(sizeof(row_ptrs1[0][0]) * cols); } /* fill and print array */ fill(rows, cols, row_ptrs1); printarray(rows, cols, row_ptrs1); /* free allocated space */ for (int r = 0; r < rows; ++r) { free(row_ptrs1[r]); } free(row_ptrs1); /* * another way to make a 2D array: * allocate one big block of space, and build pointers into it */ /* array of pointers to rows */ int **row_ptrs2 = malloc(sizeof(row_ptrs2[0]) * rows); /* space for all rows */ int *nums = malloc(sizeof(nums[0]) * rows * cols); /* fill array of pointers to rows */ for (int r = 0; r < rows; ++r) { row_ptrs2[r] = &nums[r*cols]; } /* fill and print array */ fill(rows, cols, row_ptrs2); printarray(rows, cols, row_ptrs2); /* free allocated space */ free(nums); free(row_ptrs2); return 0; }