forked from ksw2000/Data-Structure-in-C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-動態宣告二維陣列.c
50 lines (41 loc) · 1007 Bytes
/
01-動態宣告二維陣列.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <stdlib.h>
// How to simply allocate 2D array on runtime (in heap)?
int** allocate2D(int rows, int cols) {
// allocate memory for rows
int** array2D = array2D = malloc(sizeof(int*) * rows);
// allocate cols for each element in array2D
int i;
for (i = 0; i < rows; i++) {
array2D[i] = malloc(sizeof(int) * cols);
}
return array2D;
}
void free2D(int** array2D, int rows) {
int i;
for (i = 0; i < rows; i++) {
free(array2D[i]);
}
free(array2D);
}
int main() {
int rows = 9;
int cols = 5;
int** array2D = allocate2D(rows, cols);
// TEST
int i, j, count = 0;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
array2D[i][j] = count;
count++;
}
}
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%2d ", array2D[i][j]);
}
printf("\n");
}
free2D(array2D, rows);
return 0;
}