C语言实例
程序员文章站
2024-03-07 16:11:57
...
- 编译环境:Visual Studio 2012
- 编程语言:C
1、memcpy与’/0’
int main(void) {
char* p1 = "abc";
char* p2 = (char*)malloc(sizeof(char) * 3);
char* p3 = (char*)memcpy(p2, p1, 3);
printf("p2 = %s\np3 = %s\n", p2, p3);
free(p2);
p2 = NULL;
p3 = NULL;
system("pause");
return 1;
}
打印结果:乱码
p2 = abc?
p3 = abc?
请按任意键继续. . .
int main(void) {
char* p1 = "abc";
char* p2 = (char*)malloc(sizeof(char) * 4);
char* p3 = (char*)memcpy(p2, p1, 4);
printf("p2 = %s\np3 = %s\n", p2, p3);
free(p2);
p2 = NULL;
p3 = NULL;
system("pause");
return 1;
}
打印结果:正常
p2 = abc
p3 = abc
请按任意键继续. . .
上一篇: ASP.NET MVC3的伪静态实现代码