断言函数assert

#include<iostream>
#include<string.h>
#include<assert.h>
using namespace std;
int Strlen(const char* s)
{
int len = 0;
assert(s != NULL);
//非空判断是必须进行的操作,
//可以使用断言的方式assert(s) != NULL才会继续 
while (*s!= '\0')
{
len++;
s++;
}
return len;
}
int main()
{
char str[13] = "hello world!";
char* pstr = "hello world!";
char* s = "";
char* p=NULL;//1
int a[5] = { 1, 2, 3, 4, 5 };
//int* b = { 1, 2, 3, 4, 5 }; //error 指针指向的内存不确定 无效
cout << sizeof(str) << endl;
cout << sizeof(pstr) << endl;
cout << strlen(str) << endl;
cout << strlen(pstr) << endl;
cout << Strlen(str)<<endl;
cout << Strlen(pstr)<<endl;
cout << Strlen(s) << endl;
cout << Strlen(p) << endl;//2
cout << sizeof(a) << endl;
//cout << sizeof(b) << endl;
//cout << strlen(a) << endl; //error :  "int *" 类型的实参与 "const char *" 类型的形参不兼容
//cout << strlen(b) << endl;
printf(str);

有断言函数:程序挂掉了但有提示

13
4
12
12
12
12
0
Assertion failed: s != NULL, file c:\users\admin\desktop\s\数据结构\顺序栈\练习.
cpp, line 9


注释掉该函数:程序挂掉了

注释掉1,2实际输出的正确结果:

13
4
12
12
12
12
0
20
hello world!请按任意键继续. . .

http://www.cnblogs.com/ggzss/archive/2011/08/18/2145017.html





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值