当前位置:网站首页>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
边栏推荐
- MySQL winter vacation self-study 2022 11 (9)
- Sword finger offer 30 Stack containing min function
- 07 单件(Singleton)模式
- MySQL (IV) - transactions
- 会员积分营销系统操作的时候怎样提升消费者的积极性?
- 模板_求排列逆序对_基于归并排序
- Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
- 球面透镜与柱面透镜
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17
- Patch NTP server at the beginning of DDoS counterattack
猜你喜欢
Force buckle 146 LRU cache
深度解析链动2+1模式,颠覆传统卖货思维?
Pat grade a 1033 to fill or not to fill
PMP每日一练 | 考试不迷路-7.5
Li Kou today's question -729 My schedule I
Redis delete policy
Minecraft 1.16.5 biochemical 8 module version 2.0 storybook + more guns
MySQL winter vacation self-study 2022 11 (9)
[matlab] access of variables and files
Looking at the trend of sequence modeling of recommended systems in 2022 from the top paper
随机推荐
GifCam v7.0 极简GIF动画录制工具中文单文件版
Httprunnermanager installation (III) - configuring myql Database & initialization data under Linux
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 9
ftp上传文件时出现 550 Permission denied,不是用户权限问题
MySQL winter vacation self-study 2022 11 (6)
深度解析链动2+1模式,颠覆传统卖货思维?
Referenceerror: primordials is not defined error resolution
Template_ Find the reverse pair of permutations_ Sort based on merge
主数据管理(MDM)的成熟度
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 23
Microsoft speech synthesis assistant v1.3 text to speech tool, real speech AI generator
SQL table name is passed as a parameter
DDoS "fire drill" service urges companies to be prepared
Method of changing object properties
[untitled] a query SQL execution process in the database
事故指标统计
Universal crud interface
Y a - t - il des cas où sqlcdc surveille plusieurs tables et les associe à une autre? Tout fonctionne dans MySQL
Is there a case where sqlcdc monitors multiple tables and then associates them to sink to another table? All operations in MySQL