通过写简单的代码测试他们的结果
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int func1(void);
int func2(void);
int main(void)
{
func1();
printf("\n");
func2();
func3();
return 0;
}
//func1 是测试strcpy函数的功能
//是将b复制到a中,替代原来的a内容
int func1(void)
{
char a[]={'a','b','c','d'};
char b[]={'e','f','g','h','i','j','k'};
//int i = sizeof(b);
char *p;
p=strcpy(a,b);
int i = sizeof(p),j;
for(j=0;j<i;j++)
printf("%c",p[j]);
printf("\n");
puts(p);
return 1;
}
//func2 是测试strcat函数的功能
//是在a的末尾处添加b中的内容,到得a+b
int func2(void)
{
//没有这个结束符,会在a和b连接的时候出现乱码
//这个问题折腾了很久,也就是说,如果不判断dest结束的话,编译器不会自动加上
char a[]={'a','b','c','d','\0'};
char b[]={'e','f','g','h','i','j','k'};
char *p;
p = (char*)malloc(sizeof(char)*10);
p=strcat(a,b);
printf("%s\n",p);
puts(p);
return 1;
}
//测试memset的功能
//是清理内存使用的函数
int func3(void)
{
int * ptd;
ptd = (int*)malloc(sizeof(int)*6);
int j = sizeof(ptd),k;
memset(ptd,0,sizeof(ptd));
for(k=0;k<=j;j++)
printf("ptd[j]=%2d,ptd[j]=2%p",ptd[j],&ptd[j]);
return 1;
}
/************************************************************************************************************************************************
func1和func2的结果
root@ubuntu:/mnt/hgfs/winshare/C_test# ./a.out
efghijk
efghijk
abcdefghijk
abcdefghijk
fun3可以看出这段申请的连续内存被置为0,运行一段后会发生段错误,但不影响执行结果判断
ptd[j]= 0,ptd[j]=20x1673f10ptd[j]= 0,ptd[j]=20x1673f14ptd[j]= 0,ptd[j]=20x1673f18ptd[j]= 0,ptd[j]=20x1673f1cptd[j]= 0,ptd[j]=20x1673f20ptd[j]= 0,ptd[j]=20x1673f24ptd[j]= 0,ptd[j]=20x1673f28ptd[j]= 0,ptd[j]=20x1673f2cptd[j]= 0,ptd[j]=20x1673f30ptd[j]= 0,ptd[j]=20x1673f34ptd[j]= 0,ptd[j]=20x1673f38ptd[j]= 0,ptd[j]=20x1673f3cptd[j]= 0,ptd[j]=20x1673f40ptd[j]= 0,ptd[j]=20x1673f44ptd[j]= 0,ptd[j]=20x1673f48ptd[j]= 0,ptd[j]=20x1673f4cptd[j]= 0,ptd[j]=20x1673f50ptd[j]= 0,ptd[j]=20x1673f54ptd[j]= 0,ptd[j]=20x1673f58ptd[j]= 0,ptd[j]=20x1673f5cptd[j]= 0,ptd[j]=20x1673f60ptd[j]= 0,ptd[j]=20x1673f64ptd[j]= 0,ptd[j]=20x1673f68ptd[j]= 0,ptd[j]=20x1673f6cptd[j]= 0,ptd[j]=20x1673f70ptd[j]= 0,ptd[j]=20x1673f74ptd[j]= 0,ptd[j]=20x1673f78ptd[j]= 0,ptd[j]=20x1673f7cptd[j]= 0,ptd[j]=20x1673f80ptd[j]= 0,ptd[j]=20x1673f84ptd[j]= 0,ptd[j]=20x1673f88ptd[j]= 0,ptd[j]=20x1673f8cptd[j]= 0,ptd[j]=20x1673f90ptd[j]= 0,ptd[j]=20x1673f94ptd[j]= 0,ptd[j]=20x1673f98ptd[j]= 0,ptd[j]=20x1673f9cptd[j]= 0,ptd[j]=20x1673fa0ptd[j]= 0,ptd[j]=20x1673fa4ptd[j]= 0,ptd[j]=20x1673fa8ptd[j]= 0,ptd[j]=20x1673facptd[j]= 0,ptd[j]=20x1673fb0ptd[j]= 0,ptd[j]=20x1673fb4ptSegmentation fault (core dumped)
root@ubuntu:/mnt/hgfs/winshare/C_test#
************************************************************************************************************************************************/