当前位置:网站首页>C language - the difference between sizeof and strlen
C language - the difference between sizeof and strlen
2022-07-24 08:36:00 【Egg Prince】
sizeof and strlen The difference between :
1.sizeof The result type of the operator is size_t, It's in the header file typedef by unsigned int type .
This type is guaranteed to hold the byte size of the largest object created by the implementation .
2.sizeof It's an operator ,strlen Is the function .
3.sizeof You can use type as a parameter ,strlen Only use char* Make parameters , And it must be based on ''\0'' At the end of the .
sizeof You can also use functions as parameters , such as :
short f();
printf("%d\n",sizeof(f()));
The output is zero sizeof(short), namely 2.
4. Array do sizeof The parameters of do not degenerate , Pass to strlen It degenerates into a pointer .
5. Most compilers Just compile it sizeof It's been calculated It's the length of a type or variable, and that's what it is sizeof(x) The reason that can be used to define the dimension of an array
charstr[20]="0123456789";
int a=strlen(str);//a=10;
int b=sizeof(str);// and b=20;
6.strlen The results of the test can only be calculated at run time , Is used to calculate the length of a string , Not the size of the type in memory .
7.sizeof If it is a type, it must be bracketed , If it's a variable name, you can leave it out of brackets . This is because sizeof It's an operator, not a function . For variables ,sizeof( Variable name ) and sizeof Variable name , The effect is the same ; If used for type ,sizeof( type ) That's right. ,sizeof type Will be an error .
8. When applied to a structure type or variable , sizeof Returns the actual size ,
When applying a static array of spaces , sizeof Return all array sizes .
sizeof The operator cannot return the size of an array that is dynamically allocated or an external array
9. When an array is passed to a function as a parameter, it passes a pointer instead of an array , Passing is the first address of the array
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:
struct test{
int a;
int b;
int c;
};
struct test *A;
char *str1=“absde”;
char str2[]=“absde”;
char str3[8]={‘a’,};
int str4[8]={‘a’,};
char ss[] = “0123456789”;
Output :
sizeof(*A); //12, The calculated size of the whole structure .
sizeof(A); //4, Calculate the size of the pointer .
sizeof(str1); // 4, It calculates the size of pointer memory , Include ’\0’
sizeof(*str1); // 1, It calculates the size of the first element of the array
sizeof(*(str1+1)); // 1, It calculates the size of the second element of the array
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’
边栏推荐
- Using golang to implement RPC (1)
- Learn the rxjs operator
- 【一起上水硕系列】EE feedback 详解
- Enterprises love hybrid app development, and applet container technology can improve efficiency by 100%
- Wechat applet host environment, applet architecture, concise operation structure
- warning: could not execute support code to read Objective-C class data in the process.
- Dynamic programming & backtracking various deformation problems
- Advantages and disadvantages of redis and ZK implementing distributed locks
- Is it safe to open an account online in Beijing
- Read and understand move2earn project - move
猜你喜欢

How difficult is it to build a digital collection platform?

JQ native write bullet frame mask layer

What is the difference between domestic "rocket heart" artificial heart and different artificial heart?

"Solution" friend of Vulcan
![[technical interview] how to introduce yourself](/img/2e/775e4ba577098f7465309f772ee591.png)
[technical interview] how to introduce yourself
![[wechat applet development (III)] realize the stacking and sliding of cards](/img/6c/4ebd60a2106b56b8bf3a6bf17d11f9.png)
[wechat applet development (III)] realize the stacking and sliding of cards

Is yuancosmos hype? Or the future

Sed add content after a line

Typora提示【This beta version of Typora is expired, please download and install a newer version】的解决方案

Overseas media, domestic we media, media publicity
随机推荐
Golang implements sanggi diagram and analyzes user behavior trajectory
From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
Use the bark app to realize the process of pushing messages to mobile phones
Wei Xiaoli's "pursuer" is coming
Adaptive problem of img aspect ratio scaling in flex layout in Safari
Oauth2 server set up oauth2 authentication service
How difficult is it to build a digital collection platform?
Move protocol global health declaration, step into Web3 in sports
2022.7.11 overall solution
Typescript -- Generic
JS string interception
【情感】何为“优秀”
Kotlin学习笔记1——变量、函数
Introduction to wechat authorized login third-party app applet method
Online lover
[Game Collection] mobile phones are about to burst, and a collection of six high-quality pyGame games is coming ~ (source code attached)
About the big hole of wechat applet promise
T-SQL query statement
Is gamefi in decline or in the future?
Local warehouse associated with remote warehouse