关于 strcat 库函数和指针的问题
程序代码: 1 #include <stdio.h>
2 #include <string.h>
3
4 int main()
5 {
6 char *str1, *str2;
7
8 strcpy(str1, "china");
9 strcpy(str2, "fighting!");
10
11 strcat(str1, str2);
12
13 fprintf(stdout, "%s\t%s\n", str1, str2);
14
15 return 0;
16 }
17
18
19 int main()
20 {
21 char str1[20] = "china", str2[20] = "fighting!";
22
23 strcat(str1, str2);
24
25 fprintf(stdout, "%s\t%s\n", str1, str2);
26 }
上面的两个程序,使用指针的时候可以编译,但是执行的时候出现 “Segmentation fault (core dumped)”。下面的可以输出结果,这是为什么呢?上面的那个程序要如何改才能正确呢?








