回答編集履歴
3
修正
test
CHANGED
@@ -4,9 +4,9 @@
|
|
4
4
|
|
5
5
|
```C++
|
6
6
|
|
7
|
-
#include <
|
7
|
+
#include <iostream>
|
8
8
|
|
9
|
-
#include <
|
9
|
+
#include <algorithm>
|
10
10
|
|
11
11
|
#include <cstring>
|
12
12
|
|
@@ -22,15 +22,15 @@
|
|
22
22
|
|
23
23
|
|
24
24
|
|
25
|
-
str =
|
25
|
+
str = new char[std::strlen(str2)+1];
|
26
26
|
|
27
|
-
|
27
|
+
std::copy_n(str2, std::strlen(str2)+1, str);
|
28
28
|
|
29
|
-
|
29
|
+
std::cout << str;
|
30
30
|
|
31
31
|
|
32
32
|
|
33
|
-
|
33
|
+
delete[] str;
|
34
34
|
|
35
35
|
}
|
36
36
|
|
2
微修正
test
CHANGED
@@ -12,7 +12,7 @@
|
|
12
12
|
|
13
13
|
|
14
14
|
|
15
|
-
char* str =
|
15
|
+
char* str = nullptr;
|
16
16
|
|
17
17
|
|
18
18
|
|
1
追記
test
CHANGED
@@ -1 +1,37 @@
|
|
1
1
|
コピー先である str が NULL だから。
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
```C++
|
6
|
+
|
7
|
+
#include <cstdio>
|
8
|
+
|
9
|
+
#include <cstdlib>
|
10
|
+
|
11
|
+
#include <cstring>
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
char* str = NULL;
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
int main() {
|
20
|
+
|
21
|
+
const char* str2 = "aaa";
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
str = (char*)malloc(strlen(str2)+1);
|
26
|
+
|
27
|
+
memcpy(str, str2, strlen(str2)+1);
|
28
|
+
|
29
|
+
printf("%s",str);
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
free(str);
|
34
|
+
|
35
|
+
}
|
36
|
+
|
37
|
+
```
|