当前位置:网站首页>The difference between sizeof and strlen in C language
The difference between sizeof and strlen in C language
2022-07-06 02:41:00 【Mr. Xiao Yang's treasure house】
The main differences are as follows :
1、sizeof It's the operator ,strlen yes C Language standard library functions .
2、 strlen It measures the actual length of the string , With ’\0’ end , The returned result does not include ’\0’ .
3、 and sizeof It measures the allocated size of characters , Its parameters can be arrays 、 The pointer 、 type 、 object 、 Functions, etc .
To be specific , When the parameters are as follows ,sizeof The meaning of the returned value is as follows :
Array - The amount of array space allocated at compile time ;
The pointer - The amount of space used to store the pointer ;
type - The size of the space occupied by this type ;
object - The actual space occupied by the object ;
function - The space occupied by the function return type ;
Example 1:
char *str1=“absde”;
char str2[]=“absde”;
char str3[8]={‘a’,};
int str4[8]={‘a’,};
char ss[] = “0123456789”;
Output :
sizeof(str1); // 4, It calculates the size of pointer memory , Include ’\0’
sizeof(str2); // 6 , It calculates the memory size of the string , Include ’\0’
sizeof(str3); // 8 , The calculation is char Memory size of type array
sizeof(str4); // 32 , The calculation is int Memory size of type array
sizeof(ss); // 11 , It calculates the size of the string , Include ’\0’
strlen(str1); // 5 , The length of the string is calculated , barring ‘\0’
strlen(str2); // 5 , The length of the string is calculated , barring ‘\0’
strlen(str3); // ? , Because the string needs to be found ’\0’ To end , To be in ’a’ And then I found ’\0’, So it's a random value
strlen(str4); // ? , Because the string needs to be found ’\0’ To end , To be in ’a’ And then I found ’\0’, So it's a random value
strlen(ss); // 10 , The length of the string is calculated , barring ‘\0’
Example 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
right key C
Example 3
char str[]=“hello”;
char *p=str;
int n=10;
// Please calculate
sizeof(str); //6,5+1=6,1 representative ’\0’
sizeof§; //4, For the pointer
sizeof(n); //4, The storage space occupied by shaping
void func(char str[100])
{
sizeof(str); //4, here str It has been converted to a pointer
}
void *p=malloc(100);
sizeof§;//4, Pointer size
————————————————
Copyright notice : This paper is about CSDN Blogger 「 Bitter gourd 」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/qq_52368521/article/details/124950895
边栏推荐
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19
- 2.13 simulation summary
- Is there a case where sqlcdc monitors multiple tables and then associates them to sink to another table? All operations in MySQL
- 微服务注册与发现
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 22
- 事故指标统计
- [Digital IC manual tearing code] Verilog asynchronous reset synchronous release | topic | principle | design | simulation
- Apt installation ZABBIX
- Redis cluster deployment based on redis5
- Large scale DDoS attacks take Myanmar offline
猜你喜欢
Pure QT version of Chinese chess: realize two-man, man-machine and network games
RobotFramework入门(二)appUI自动化之app启动
Universal crud interface
力扣今日題-729. 我的日程安排錶 I
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 13
PMP每日一练 | 考试不迷路-7.5
Deeply analyze the chain 2+1 mode, and subvert the traditional thinking of selling goods?
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 18
HttpRunnerManager安装(三)-Linux下配置myql数据库&初始化数据
Advanced technology management - what is the physical, mental and mental strength of managers
随机推荐
Is there a case where sqlcdc monitors multiple tables and then associates them to sink to another table? All operations in MySQL
零基础自学STM32-野火——GPIO复习篇——使用绝对地址操作GPIO
MySQL winter vacation self-study 2022 11 (9)
MySQL winter vacation self-study 2022 11 (6)
2022 China eye Expo, Shandong vision prevention and control exhibition, myopia, China myopia correction Exhibition
How to accurately identify master data?
ReferenceError: primordials is not defined错误解决
Referenceerror: primordials is not defined error resolution
2020.02.11
Bigder:34/100 面试感觉挺好的,没有收到录取
Solution: attributeerror: 'STR' object has no attribute 'decode‘
Ue4- how to make a simple TPS role (II) - realize the basic movement of the role
Spherical lens and cylindrical lens
Accident index statistics
Easy to use js script
HttpRunnerManager安装(三)-Linux下配置myql数据库&初始化数据
从顶会论文看2022年推荐系统序列建模的趋势
2.13 simulation summary
有沒有sqlcdc監控多張錶 再關聯後 sink到另外一張錶的案例啊?全部在 mysql中操作
主数据管理理论与实践