|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int main()
{
char str[] = "MengLiang";
//此处分配空间没有考虑到'\0'
char* New_str = (char*)malloc(strlen(str));
strcpy(New_str, str);
printf("The New_str = %s\n", New_str);
free(New_str);
New_str = NULL;
system("pause");
return 0;
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int main()
{
char str[] = "MengLiang";
//此处的加1就是为'\0'来服务的
char* New_str = (char*)malloc(strlen(str)+1);
strcpy(New_str, str);
printf("The New_str = %s\n", New_str);
free(New_str);
New_str = NULL;
system("pause");
return 0;
}
|