当前位置:网站首页>Common mistakes in C language (sizeof and strlen)
Common mistakes in C language (sizeof and strlen)
2022-06-23 01:49:00 【Nobaya】
One 、sizeof and strlen The difference between
sizeof yes C A key word in a language , and strlen It's a library function ( Included in <string.h> in ), The functions of the two seem very similar , however , It makes a big difference .
- strlen( Can only ) Is used to measure the length of a string ( It does not include the... That marks the end of the string ‘’).
- and sizeof Is to calculate an object ( It can be a variable or a data type ) Size in space , That is, the size of the occupied space .
About strlen You can refer to the information here : strlen A detailed description
Two 、sizeof and strlen Application scenarios of
First of all, let's look at a topic .
int num[20] = { 1 , 2, 3, 4, 5, 6, 7, 8, 9, 10 };char arr[50] = "abcdefg001"; seek arr The length and size of the space . seek num Number of elements and size of space .Think about this topic is not very simple .
Ask for in this topic num The number of elements can be used sizeof(num) / sizeof(num[0]) To get , Now sizeof(num) Represents the total space occupied by this array ( That is, the length of the data type * Element number ), and sizeof(num[0]) Represents the space occupied by the data type of this array . Let's take a look at the answer :
You can see :
- strlen In use, only '' Preceding characters . That's the number of characters .
- sizeof When used, it calculates how much space this object occupies in memory .
matters needing attention :
- Although the use of sizeof You can find the number of elements of an array , But you can't use it across functions , Because an array will only pass The first address of the array Transfer the past . In this way, you can use sizeof(num) / sizeof(num[0]) Will only get 1.
- strlen Can be used across functions to find the number of elements , because strlen The string length() function evaluates the length of a string , Yes ' ' You can specify the end of a string .
边栏推荐
- Score and loop statements (including goto statements) -part3
- 278. digital combination
- SQL programming task03 job - more complex query
- Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
- Charles garbled code problem solving
- Uint8 serializing and deserializing pits using stringstream
- On AI and its future trend | community essay solicitation
- "Initial C language" (Part 1)
- You can also do NLP (classification)
- Debian10 configuring rsyslog+loganalyzer log server
猜你喜欢

Debian10 LVM logical volumes

LeetCode 206. 反转链表(迭代+递归)

On AI and its future trend | community essay solicitation

Network module packaging

Cmake simple usage

How to download online printing on Web pages to local PDF format (manual personal test)

Function part

Three methods for solving Fibonacci sequence feibonacci (seeking rabbit) - program design

JS - single sign on

ERROR { err: YAMLException: end of the stream or a document separator is expected at line 6, colum
随机推荐
6. const usage, combination of first and second level pointers
Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;
[template] KMP
//1.8 char character variable assignment integer
Wechat mobile terminal development - account login authorization
Freshman C language summary post (hold change) Part1 output diamond
3D打印微组织
Pat class A - 1014 waiting in line (bank queuing problem | queue+ simulation)
C# SerializableDictionary序列化/反序列化
Detailed explanation of makefile usage
//1.7 use of escape characters
Pat class a 1016 phone bills (time difference)
Debian10 configuring rsyslog+loganalyzer log server
崔鹏团队:万字长文梳理「稳定学习」全景图
Using WordPress to create a MySQL based education website (learning notes 2) (technical notes 1) xampp error1045 solution
Pat class A - 1012 the best rank (PIT)
Detailed explanation of GCC usage
Day260: the number III that appears only once
How are pub and sub connected in ros1?
How to type Redux actions and Redux reducers in TypeScript?
