当前位置:网站首页>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
边栏推荐
- ReferenceError: primordials is not defined错误解决
- Referenceerror: primordials is not defined error resolution
- 一位博士在华为的22年
- 力扣今日题-729. 我的日程安排表 I
- 数据准备工作
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 22
- 构建库函数的雏形——参照野火的手册
- Data preparation
- CobaltStrike-4.4-K8修改版安装使用教程
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14
猜你喜欢
A copy can also produce flowers
RobotFramework入门(三)WebUI自动化之百度搜索
【MySQL 15】Could not increase number of max_ open_ files to more than 10000 (request: 65535)
[Digital IC manual tearing code] Verilog asynchronous reset synchronous release | topic | principle | design | simulation
Déduisez la question d'aujourd'hui - 729. Mon emploi du temps I
Maturity of master data management (MDM)
PMP每日一练 | 考试不迷路-7.5
Universal crud interface
Gifcam v7.0 minimalist GIF animation recording tool Chinese single file version
Structural theme model (I) STM package workflow
随机推荐
PMP每日一练 | 考试不迷路-7.5
Thinking on Architecture Design (under continuous updating)
ReferenceError: primordials is not defined错误解决
[matlab] access of variables and files
故障分析 | MySQL 耗尽主机内存一例分析
GifCam v7.0 极简GIF动画录制工具中文单文件版
Is there a case where sqlcdc monitors multiple tables and then associates them to sink to another table? All operations in MySQL
07 单件(Singleton)模式
HDU_ p1237_ Simple calculator_ stack
RobotFramework入门(三)WebUI自动化之百度搜索
Introduction to robotframework (II) app startup of appui automation
剑指 Offer 29. 顺时针打印矩阵
LeetCode 103. Binary tree zigzag level order transverse - Binary Tree Series Question 5
How to read excel, PDF and JSON files in R language?
CobaltStrike-4.4-K8修改版安装使用教程
[Wu Enda machine learning] week5 programming assignment EX4 - neural network learning
MySQL winter vacation self-study 2022 11 (6)
Redis installation
[Digital IC manual tearing code] Verilog asynchronous reset synchronous release | topic | principle | design | simulation
Initial understanding of pointer variables