当前位置:网站首页>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
边栏推荐
- Looking at the trend of sequence modeling of recommended systems in 2022 from the top paper
- HDU_p1237_简单计算器_stack
- DDoS "fire drill" service urges companies to be prepared
- 事故指标统计
- Zero foundation self-study STM32 - Review 2 - encapsulating GPIO registers with structures
- Differences and usage scenarios between TCP and UDP
- UE4 - how to make a simple TPS role (I) - create a basic role
- PAT甲级 1033 To Fill or Not to Fill
- Déduisez la question d'aujourd'hui - 729. Mon emploi du temps I
- There are so many giants, why should we independently develop POS store cashier system?
猜你喜欢
Which ecology is better, such as Mi family, graffiti, hilink, zhiting, etc? Analysis of five mainstream smart brands
GifCam v7.0 极简GIF动画录制工具中文单文件版
剑指 Offer 29. 顺时针打印矩阵
[untitled] a query SQL execution process in the database
米家、涂鸦、Hilink、智汀等生态哪家强?5大主流智能品牌分析
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
LeetCode 103. Binary tree zigzag level order transverse - Binary Tree Series Question 5
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 12
[matlab] access of variables and files
纯Qt版中国象棋:实现双人对战、人机对战及网络对战
随机推荐
Paper notes: graph neural network gat
550 permission denied occurs when FTP uploads files, which is not a user permission problem
力扣今日题-729. 我的日程安排表 I
Crawler (9) - scrape framework (1) | scrape asynchronous web crawler framework
【MySQL 15】Could not increase number of max_ open_ files to more than 10000 (request: 65535)
After changing the GCC version, make[1] appears in the compilation: cc: command not found
SSM 程序集
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 6
Template_ Quick sort_ Double pointer
Paper notes: limit multi label learning galaxc (temporarily stored, not finished)
力扣今日題-729. 我的日程安排錶 I
[untitled] a query SQL execution process in the database
HDU_ p1237_ Simple calculator_ stack
MySQL winter vacation self-study 2022 11 (5)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
纯Qt版中国象棋:实现双人对战、人机对战及网络对战
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21
2022.02.13
Looking at the trend of sequence modeling of recommended systems in 2022 from the top paper
[postgraduate entrance examination English] prepare for 2023, learn list5 words