当前位置:网站首页>[C language] relevant distinction between strlen and sizeof
[C language] relevant distinction between strlen and sizeof
2022-07-27 02:24:00 【Helinliupi he】
Blog home page :Luo-Kuang- What
motto : On the way to the peak of life together
Learning progress :【C Language 】
Blog statement : I will try my best to , Write every blog carefully , Let more friends exchange learning experiences with me .
If there is any deficiency , Please correct me. . Learning is the process of constantly making mistakes and correcting !
Preface :
We often need to know the length of some arrays to calculate or find a value , Usually I use strlen Functions and sizeof To get the results we want .
Define the distinction between :
1. First , Let me start with strlen And sizeof The difference between definitions
strlen It's a library function ( need #include<stdlib.h> The header file )
strlen Is to find the length of the string , Whether there is... In the string of interest \0, Statistics \0 The number of characters that appear before
sizeof It's the operator
sizeof Only pay attention to how much memory space is occupied , Don't pay attention to the data stored in memory
Code display :
1. First show some related code
2. Running results
Problem description :
I can see , The result may not be the same as we expected .
1. The first is also strlen, but 2 Array initialization methods are different , The results are also different .
2. The same is sizeof,2 The results of array initialization methods are also different , also arr1 It produced an unexpected number 15.
2. arr1 And arr2 The difference between
char arr1[] = "abc"; by : a b c \0
char arr2[] = { 'a', 'b', 'c' }; by : a b c
3. There are different reasons
First :arr1 Initialize to string , And the string should use \0 ending , therefore arr1 The string will be automatically supplemented with \0
arr2 Initialize to the specified 3 Characters , Unwanted \0 ending , It won't make up \0
Result analysis :
1. Yes strlen The results were analyzed differently
according to strlen Definition , Statistics \0 The number of characters before .
therefore arr1 The result is 3
because arr2 Not in it \0, According to the memory relationship , Will go to arr2 Open up the memory and look for \0, Until I find \0, Statistics \0 The number of characters before , It will be a random value , This time for 15
2. Yes sizeof The results were analyzed differently
according to sizeof Definition , Count the number of bytes in the array (sizeof The unit of return value is bytes )
arr1 Array \0 It's also a character , And there are bytes , To be counted . therefore arr1 by 4 Byte space
arr2 There is no \0, And only 3 Character station 3 Byte space
summary
1. Array initialization ( In undetermined space size ) The string will be automatically added \0 End with
For multiple characters , I won't add \0 ending
2.strlen To statistics \0 The number of characters before ( It doesn't contain \0, And must find \0) Can only be used to find the length of a string
sizeof Only count the number of bytes contained in the space of the array ( contain \0, because \0 It's also a character , Station one byte ) String length -1 Give a matrix ( In especial int Type an array of single numbers ) All available
Conclusion :
Dear friends , If you think it's useful, just give it to Bo Sanlian ! If there is a mistake , Please correct me. , Thank you. ! If you have different opinions , Please communicate with me , Progress together
We met at the summit !!!️
边栏推荐
- HCIP第一天
- The latest C language introduction and advanced - the most complete and detailed C language tutorial in history!! Section 1 - overview of C language
- C语言——赋值运算符、复合的赋值运算符、自增自减运算符、逗号运算符、条件运算符、goto语句、注释
- Lvs+keepalived project practice
- Error handling in golang
- Codeforces Round #809 (Div. 2), problem: (C) Qpwoeirut And The City
- Lecture 3 - GPIO input / output library function usage and related routines
- First knowledge of Web Design
- 数字集成电路:MOS管器件章(二)
- Hcip day 1
猜你喜欢
随机推荐
OSPF configuration in mGRE environment and LSA optimization - reduce the amount of LSA updates (summary, special areas)
(the most detailed in History) codeforces round 805 (Div. 3) e Split Into Two Sets
Static routing default routing VLAN experiment
指针得真正奥义!!!
NAT网络地址转化实验
HCIP-第六天-OSPF静态大实验
C语言——数组、字符串处理函数、strlen、strcpy和strncpy、strcat和strncat、strcmp和strncmp
RISC-V工具链编译笔记
HCIP oSPF综合实验
Republishing and routing strategy of OSPF
C language - value range of data type and basic data type
Golang — 解析 yaml 文件
Golang bufio Reader 源码详解
HCIP-第五天-OSPF扩展配置实验
NAT network address translation experiment
Tcp的三次握手与四次挥手
HandsomeForum学习论坛
C language - characters and strings, arithmetic operators, type conversions
Codeforces Round #809 (Div. 2), problem: (C) Qpwoeirut And The City
【C语言】strlen与sizeof相关区分









