File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* calloc, realloc 사용해서 매모리 동적 할당 받기 */
2
+
3
+ #include <stdio.h>
4
+ #include <stdlib.h>
5
+
6
+
7
+
8
+ int main (void )
9
+ {
10
+ struct arr {
11
+ int size ;
12
+ int max_size ;
13
+ int * array ;
14
+ } * lpArray ;
15
+
16
+ lpArray = (struct arr * )malloc (sizeof (struct arr ));
17
+ lpArray -> size = 0 ;
18
+ lpArray -> max_size = 5 ;
19
+ lpArray -> array = (int * )calloc (lpArray -> max_size , sizeof (int ));
20
+
21
+ while (1 )
22
+ {
23
+ if (lpArray -> size >= lpArray -> max_size )
24
+ {
25
+ printf ("array is full!!\n" );
26
+ lpArray -> max_size += 5 ;
27
+ lpArray -> array = (int * )realloc (lpArray -> array , lpArray -> max_size * sizeof (int ));
28
+ }
29
+
30
+ scanf ("%d" , & lpArray -> array [lpArray -> size ]);
31
+ if (lpArray -> array [lpArray -> size ] == -1 ) break ;
32
+ lpArray -> size ++ ;
33
+ }
34
+
35
+ int i ;
36
+ for (i = 0 ; i < lpArray -> size ; i ++ )
37
+ {
38
+ printf ("%d\t" , lpArray -> array [i ]);
39
+ }
40
+ printf ("\n" );
41
+
42
+ free (lpArray -> array );
43
+ free (lpArray );
44
+
45
+ return 0 ;
46
+ }
You can’t perform that action at this time.
0 commit comments