当前位置:网站首页>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
边栏推荐
- 2022 edition illustrated network pdf
- MySQL winter vacation self-study 2022 11 (8)
- Paper notes: graph neural network gat
- Black high-end responsive website dream weaving template (adaptive mobile terminal)
- SSM assembly
- DDoS "fire drill" service urges companies to be prepared
- 大厂镜像库
- 模板_快速排序_双指针
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 6
- MySQL winter vacation self-study 2022 11 (9)
猜你喜欢

爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架

Looking at the trend of sequence modeling of recommended systems in 2022 from the top paper

Paper notes: graph neural network gat

Ue4- how to make a simple TPS role (II) - realize the basic movement of the role

Pat grade a 1033 to fill or not to fill

GifCam v7.0 极简GIF动画录制工具中文单文件版
![[postgraduate entrance examination English] prepare for 2023, learn list5 words](/img/6d/47b853e76d1757fb6e42c2ebba38af.jpg)
[postgraduate entrance examination English] prepare for 2023, learn list5 words

QT release exe software and modify exe application icon

Minecraft 1.16.5 biochemical 8 module version 2.0 storybook + more guns

Easy to use js script
随机推荐
主数据管理理论与实践
Shell script updates stored procedure to database
3D drawing ()
2022 China eye Expo, Shandong vision prevention and control exhibition, myopia, China myopia correction Exhibition
Day 50 - install vsftpd on ceontos6.8
Building the prototype of library functions -- refer to the manual of wildfire
Paper notes: graph neural network gat
RobotFramework入门(一)简要介绍及使用
Microsoft speech synthesis assistant v1.3 text to speech tool, real speech AI generator
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 7
爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架
Which ecology is better, such as Mi family, graffiti, hilink, zhiting, etc? Analysis of five mainstream smart brands
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 20
"Hands on learning in depth" Chapter 2 - preparatory knowledge_ 2.3 linear algebra_ Learning thinking and exercise answers
Reset nodejs of the system
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 13
Sword finger offer 29 Print matrix clockwise
MySQL winter vacation self-study 2022 11 (5)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 24
After changing the GCC version, make[1] appears in the compilation: cc: command not found