对比C++中sizeof与strlen函数的区别
2009-09-30 22:06
232 查看
1. sizeof 操作符的结果类型size_t,它在头文件中typedef为unsigned int类型: typedef unsigned int size_t. 2. sizeof是是长度运算符, 获得数据类型或是变量的长度,如果是数据类型,则返回数据类型大小,如果是用数组,则返回数组所占空间大小,strlen是计算字符串长度的函数,返回的是实际串长度,以char* 作参数 ,且必须是以'/0'结尾。
3. sizeof在编译的时候就把计算过,strlen的结果要在运行的时候才能计算出来。
4. 数组做长度运算符sizeof的参数不退化。数组做函数strlen的参数就退化为指针了,因为数组作为参数传给函数时传的是指针而不是数组,传递的是数组的首地址。
char* ss = "0123456789";
cout<<sizeof(ss)<<endl;//4
cout<<sizeof(*ss)<<endl;//1
cout<<strlen(ss)<<endl;//10
char ss1[] = "0123456789";
cout<<sizeof(ss1)<<endl;//11
cout<<sizeof(*ss1)<<endl;//1
cout<<strlen(ss1)<<endl;//10
char ss2[100] = "0123456789";
cout<<sizeof(ss2)<<endl;//100
char ss3[] = "0123456789 ";
cout<<sizeof(ss3)<<endl;//12
int n[4] = {1,2,3,4};
cout<<sizeof(n)<<endl;//16
int n1= 1234;
cout<<sizeof(n1)<<endl;//4
3. sizeof在编译的时候就把计算过,strlen的结果要在运行的时候才能计算出来。
4. 数组做长度运算符sizeof的参数不退化。数组做函数strlen的参数就退化为指针了,因为数组作为参数传给函数时传的是指针而不是数组,传递的是数组的首地址。
char* ss = "0123456789";
cout<<sizeof(ss)<<endl;//4
cout<<sizeof(*ss)<<endl;//1
cout<<strlen(ss)<<endl;//10
char ss1[] = "0123456789";
cout<<sizeof(ss1)<<endl;//11
cout<<sizeof(*ss1)<<endl;//1
cout<<strlen(ss1)<<endl;//10
char ss2[100] = "0123456789";
cout<<sizeof(ss2)<<endl;//100
char ss3[] = "0123456789 ";
cout<<sizeof(ss3)<<endl;//12
int n[4] = {1,2,3,4};
cout<<sizeof(n)<<endl;//16
int n1= 1234;
cout<<sizeof(n1)<<endl;//4
相关文章推荐
- sizeof()运算符与strlen()函数的区别
- C++基础--sizeof和strlen的区别
- 关于sizeof运算符和strlen()函数的个别区别分析
- C/C++之sizeof与strlen的区别
- C/C++中 sizeof()与strlen()的区别
- C/C++ sizeof与strlen区别
- C++中strlen与sizeof的区别
- C++-sizeof()与strlen()的区别
- C++中strlen与sizeof的区别
- 【C++基础】sizeof 与 strlen的区别
- C++中sizeof与strlen的区别
- C/C++之sizeof与strlen的区别
- C++开发面试题:sizeof与strlen区别
- C/C++学习之strlen与sizeof区别、阿里巴巴笔试题实例
- 看过来,strlen()函数与sizeof的区别?????
- C/C++中字符串长度strlen()/sizeof()区别
- C语言学习5:数组定义,数组名的含义,数组作为函数参数,数组赋值,数组左右移动,最大最小,反向遍历,冒泡排序,sizeof 和strlen区别,gets和puts,strcpy,strcat,strcmp,strchr,strstr,strlen
- sizeof()于strlen()函数的区别
- C/C++ sizeof与strlen区别
- sizeof与strlen函数的区别