回答編集履歴
2
ソース修正
test
CHANGED
@@ -70,7 +70,7 @@
|
|
70
70
|
|
71
71
|
free(mojiretu);
|
72
72
|
|
73
|
-
break
|
73
|
+
continue; // break→continue
|
74
74
|
|
75
75
|
}
|
76
76
|
|
@@ -94,20 +94,30 @@
|
|
94
94
|
|
95
95
|
usr~/test/c % ./a.out
|
96
96
|
|
97
|
+
文字列を入力:1234567
|
98
|
+
|
99
|
+
領域を超えています。
|
100
|
+
|
101
|
+
文字列を入力:abc
|
102
|
+
|
103
|
+
入力された文字列はabcです。
|
104
|
+
|
97
105
|
文字列を入力:1234
|
98
106
|
|
99
107
|
入力された文字列は1234です。
|
100
|
-
|
101
|
-
文字列を入力:abcd
|
102
|
-
|
103
|
-
入力された文字列はabcdです。
|
104
108
|
|
105
109
|
文字列を入力:12345
|
106
110
|
|
107
111
|
領域を超えています。
|
108
112
|
|
113
|
+
文字列を入力:0
|
114
|
+
|
115
|
+
入力された文字列は0です。
|
116
|
+
|
117
|
+
文字列を入力:^C
|
118
|
+
|
109
119
|
usr~/test/c %
|
110
120
|
|
121
|
+
```
|
111
122
|
|
112
|
-
|
113
|
-
|
123
|
+
一部修正しました、ただ、これだとメモリ確保に失敗しないと終了しません、
|
1
追記
test
CHANGED
@@ -9,3 +9,105 @@
|
|
9
9
|
あと・・・メモリが確保できなというのは考えづらい。
|
10
10
|
|
11
11
|
ちなみにうちの機械(linux:mem16GB)では、8GBぐらいなら余裕で確保できます。
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
「追記」
|
16
|
+
|
17
|
+
繰り返し入力&メモリ確保失敗と入力長異常に対応(題意にそぐわないなら無視希望)
|
18
|
+
|
19
|
+
```c
|
20
|
+
|
21
|
+
usr~/test/c % cat ct.c
|
22
|
+
|
23
|
+
#include<stdio.h>
|
24
|
+
|
25
|
+
#include<stdlib.h>
|
26
|
+
|
27
|
+
#include<string.h>
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
#define Forever for(;;)
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
int main(void)
|
36
|
+
|
37
|
+
{
|
38
|
+
|
39
|
+
char *mojiretu;
|
40
|
+
|
41
|
+
char buf[100];
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
Forever{
|
46
|
+
|
47
|
+
mojiretu = (char *)malloc(5);
|
48
|
+
|
49
|
+
if (mojiretu == NULL)
|
50
|
+
|
51
|
+
{
|
52
|
+
|
53
|
+
puts("メモリ確保失敗。");
|
54
|
+
|
55
|
+
break;
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
printf("文字列を入力:");
|
62
|
+
|
63
|
+
fgets(buf,sizeof buf - 1, stdin);
|
64
|
+
|
65
|
+
size_t len= strlen(buf);
|
66
|
+
|
67
|
+
if( len > 5 ){
|
68
|
+
|
69
|
+
puts("領域を超えています。");
|
70
|
+
|
71
|
+
free(mojiretu);
|
72
|
+
|
73
|
+
break;
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
buf[len-1]= '\0'; // 改行を削除
|
78
|
+
|
79
|
+
strcpy(mojiretu,buf);
|
80
|
+
|
81
|
+
printf("入力された文字列は%sです。\n", mojiretu);
|
82
|
+
|
83
|
+
free(mojiretu);
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
return 0;
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
usr~/test/c % ./a.out
|
96
|
+
|
97
|
+
文字列を入力:1234
|
98
|
+
|
99
|
+
入力された文字列は1234です。
|
100
|
+
|
101
|
+
文字列を入力:abcd
|
102
|
+
|
103
|
+
入力された文字列はabcdです。
|
104
|
+
|
105
|
+
文字列を入力:12345
|
106
|
+
|
107
|
+
領域を超えています。
|
108
|
+
|
109
|
+
usr~/test/c %
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
```
|