C语言assert(断言)用法
//与if用法一样,如果其值为假(即为0),那么它先向stderr打印一条出错信息,然后通过调用abort来终止程序运行
//使用assert的缺点是,频繁使用会极大影响程序的性能,增加额外的开销
#include<stdio.h>#include <assert.h>
char* myStrcpy(char *des,char *src)
{
assert(des!=NULL&&src!=NULL);
char *bak=des;
while(*src!='\0'){
*des=*src;
des++;
src++;
}
des='\0';
return bak;
}
int main()
{
char str[128]={'\0'};
char *pstr=NULL;
char *p="chenlichen handsome";
myStrcpy(pstr,p);//传参,传给自行实现copy的函数从*p copy数值给str,
puts(str);
return 0;
}
运行结果为
D:\代码\第七章>a.exe
ok
Assertion failed!
Program: D:\\\a.exe
File: demo_assert.c, Line 8
Expression: des!=NULL&&src!=NULL