当前位置:网站首页>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
边栏推荐
- sql表名作为参数传递
- LeetCode 103. Binary tree zigzag level order transverse - Binary Tree Series Question 5
- Pat grade a 1033 to fill or not to fill
- MySQL winter vacation self-study 2022 11 (7)
- 2020.02.11
- 力扣今日題-729. 我的日程安排錶 I
- [postgraduate entrance examination English] prepare for 2023, learn list5 words
- SQL table name is passed as a parameter
- 好用的 JS 脚本
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 10
猜你喜欢
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17](/img/85/2635afeb2edeb0f308045edd1f3431.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17

GifCam v7.0 极简GIF动画录制工具中文单文件版

如何精准识别主数据?

Apt installation ZABBIX
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 10](/img/89/1c2f98973b79e8d181c10d7796fbb5.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 10
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16](/img/c3/f3746b161012acc3751b2bd0b8f663.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16

2022 China eye Expo, Shandong vision prevention and control exhibition, myopia, China myopia correction Exhibition

Network Security Learning - Web vulnerabilities (Part 1)

零基础自学STM32-复习篇2——使用结构体封装GPIO寄存器

Sword finger offer 29 Print matrix clockwise
随机推荐
GifCam v7.0 极简GIF动画录制工具中文单文件版
Qt发布exe软件及修改exe应用程序图标
[matlab] access of variables and files
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 22
一位博士在华为的22年
729. My schedule I / offer II 106 Bipartite graph
HttpRunnerManager安装(三)-Linux下配置myql数据库&初始化数据
有沒有sqlcdc監控多張錶 再關聯後 sink到另外一張錶的案例啊?全部在 mysql中操作
从顶会论文看2022年推荐系统序列建模的趋势
How to check the lock information in gbase 8C database?
Zero foundation self-study STM32 - Review 2 - encapsulating GPIO registers with structures
零基础自学STM32-野火——GPIO复习篇——使用绝对地址操作GPIO
MySQL winter vacation self-study 2022 11 (8)
[coppeliasim] efficient conveyor belt
Template_ Quick sort_ Double pointer
Bigder:34/100 面试感觉挺好的,没有收到录取
Redis delete policy
How to improve the enthusiasm of consumers when the member points marketing system is operated?
剑指 Offer 30. 包含min函数的栈
Six stone management: why should leaders ignore product quality