#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);
#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