当前位置:网站首页>C语言sizeof和strlen的区别
C语言sizeof和strlen的区别
2022-07-06 02:36:00 【小阳先生的宝库】
主要区别如下:
1、sizeof是运算符,strlen是C语言标准库函数。
2、 strlen 测量的是字符串的实际长度,以’\0’ 结束,返回结果不包括’\0’ 。
3、而sizeof 测量的是字符的分配大小,它的参数可以是数组、指针、类型、对象、函数等。
具体而言,当参数分别如下时,sizeof返回的值含义如下:
数组-编译时分配的数组空间大小;
指针-存储该指针所用的空间大小;
类型-该类型所占的空间的大小;
对象-对象的实际占用空间大小;
函数-函数返回类型所占空间的大小;
例题1:
char *str1=“absde”;
char str2[]=“absde”;
char str3[8]={‘a’,};
int str4[8]={‘a’,};
char ss[] = “0123456789”;
输出:
sizeof(str1); // 4,计算的是指针内存的大小,包括’\0’
sizeof(str2); // 6 ,计算的是字符串的内存大小,包括’\0’
sizeof(str3); // 8 ,计算的是char型数组的内存大小
sizeof(str4); // 32 ,计算的是int型数组的内存大小
sizeof(ss); // 11 ,计算的是字符串的大小,包括’\0’
strlen(str1); // 5 ,计算的是字符串长度,不包括‘\0’
strlen(str2); // 5 ,计算的是字符串长度,不包括‘\0’
strlen(str3); // ? ,因为字符串需要找到’\0’才可结束,要在’a’之后找到’\0’,所以是个随机值
strlen(str4); // ? ,因为字符串需要找到’\0’才可结束,要在’a’之后找到’\0’,所以是个随机值
strlen(ss); // 10 ,计算的是字符串长度,不包括‘\0’
例题2:
what is the output of this code?
char s[20]=“Hello\0Hi”;
printf(“%d %d”,strlen(s),sizeof(s));
A 5 9
B 7 20
C 5 20
D 8 20
正确答案 C
例题3
char str[]=“hello”;
char *p=str;
int n=10;
//请计算
sizeof(str); //6,5+1=6,1代表’\0’
sizeof§; //4,代表指针
sizeof(n); //4,整形占据的存储空间
void func(char str[100])
{
sizeof(str); //4,此时str已经转换为指针了
}
void *p=malloc(100);
sizeof§;//4,指针大小
————————————————
版权声明:本文为CSDN博主「贤瓜苦」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_52368521/article/details/124950895
边栏推荐
- SSM 程序集
- How to generate rich text online
- 550 permission denied occurs when FTP uploads files, which is not a user permission problem
- Sword finger offer 30 Stack containing min function
- 2022 eye health exhibition, vision rehabilitation exhibition, optometry equipment exhibition, eye care products exhibition, eye mask Exhibition
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14
- PMP每日一练 | 考试不迷路-7.5
- How to check the lock information in gbase 8C database?
- Force buckle 146 LRU cache
- Thinking on Architecture Design (under continuous updating)
猜你喜欢
Microsoft speech synthesis assistant v1.3 text to speech tool, real speech AI generator
Apt installation ZABBIX
RobotFramework入门(二)appUI自动化之app启动
2345文件粉碎,文件强力删除工具无捆绑纯净提取版
米家、涂鸦、Hilink、智汀等生态哪家强?5大主流智能品牌分析
纯Qt版中国象棋:实现双人对战、人机对战及网络对战
QT release exe software and modify exe application icon
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 23
零基础自学STM32-复习篇2——使用结构体封装GPIO寄存器
Lecture 4 of Data Engineering Series: sample engineering of data centric AI
随机推荐
How to generate rich text online
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
Follow the mouse's angle and keyboard events
Template_ Quick sort_ Double pointer
从顶会论文看2022年推荐系统序列建模的趋势
微软语音合成助手 v1.3 文本转语音工具,真实语音AI生成器
UE4 - how to make a simple TPS role (I) - create a basic role
Looking at the trend of sequence modeling of recommended systems in 2022 from the top paper
Y a - t - il des cas où sqlcdc surveille plusieurs tables et les associe à une autre? Tout fonctionne dans MySQL
【MySQL 15】Could not increase number of max_ open_ files to more than 10000 (request: 65535)
"Hands on learning in depth" Chapter 2 - preparatory knowledge_ 2.3 linear algebra_ Learning thinking and exercise answers
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21
Formatting occurs twice when vs code is saved
一个复制也能玩出花来
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 22
High number_ Vector algebra_ Unit vector_ Angle between vector and coordinate axis
Briefly describe the implementation principle of redis cluster
Thinking on Architecture Design (under continuous updating)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19
Sword finger offer 30 Stack containing min function