当前位置:网站首页>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 .
边栏推荐
- [luogu] p1083 [noip2012 improvement group] borrow classroom (line segment tree)
- Char[], char *, conversion between strings
- Detailed explanation of makefile usage
- On AI and its future trend | community essay solicitation
- SYSTEMd summary
- Why can't I access object properties in a for in loop in an object array
- Module 8 job
- SQL programming task06 assignment - Autumn recruit secret script ABC
- Pat class A - 1007 maximum subsequence sum
- [cmake command notes]target_ compile_ options
猜你喜欢

Network module packaging

Module 8 job

Cmake simple usage

Unique in Pimpl_ PTR compilation errors and Solutions

MySQL -- how to access the database of a computer in the same LAN (prenatal education level teaching)

Charles garbled code problem solving

Day367: valid complete square

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

SQL programming task03 job - more complex query

JS to paste pictures into web pages
随机推荐
[learning notes] roll back Mo team
Binary String
Use elk to save syslog, NetFlow logs and audit network interface traffic
Triangle judgment (right angle, equilateral, general)
Quick sort method
C. Number of Pairs-Codeforces Round #725 (Div. 3)
Unit of RMB in words
Branch and loop statements (including goto statements) -part1
Pat class a 1016 phone bills (time difference)
MySQL -- how to access the database of a computer in the same LAN (prenatal education level teaching)
//1.11 basic operators
Time complexity
Pat class A - 1014 waiting in line (bank queuing problem | queue+ simulation)
总体标准差和样本标准差
SQL programming task05 job -sql advanced processing
C# SerializableDictionary序列化/反序列化
Knowledge point learning
9. class and object practice and initialization list
"First knowledge of C language" (Part 3)
Pat class A - 1015 reversible primes
