Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse sizeof and strlen

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)05/31 Report--

The content of this article mainly focuses on how to analyze sizeof and strlen. The content of the article is clear and clear. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!

1.strlen function.

All strlen does is a counter that scans from a location in memory (which can be the beginning of a string, a location in the middle, or even an uncertain area of memory) until the first string Terminator'\ 0' is encountered, and then returns the counter value.

2.sizeof operator

Sizeof () returns the amount of memory occupied after the variable is declared, not the actual length, and sizeof is not a function, just an operator.

The difference between 3.strlen and sizeof

3.1Types can be used as parameters for sizeof, and char* can only be used for strlen, and it must end with'\ 0arguments'.

3.2 sizeof can also take functions as parameters, such as: short f (); printf ("% d\ n", sizeof (f (); the output is sizeof (short), that is, 2.

The parameters of the array to do sizeof do not degenerate, and the parameters passed to strlen will be reduced to pointers.

Most compilers calculate whether sizeof is a type or the length of a variable at compile time, which is why sizeof (x) can be used to define the dimension of an array.

The result of 3.5 strlen can only be calculated at run time, and is used to calculate the length of the string, not the size of the type in memory.

3.6After sizeof, the type must be parenthesized, and the variable name can be unbracketed. This is because sizeof is an operator, not a function.

4. Give an example to distinguish between strlen and sizeof

1.char str [20] = "0123456789"

Int a=strlen (str); / / axi10; > strlen calculates the length of the string, ending with the Terminator 0x00 as the string.

Int b=sizeof (str); / / and bread20; > sizeof calculates the amount of memory space occupied by the allocated array str [20], regardless of the contents stored in it.

two。 The above is the result of processing the static array. If it is for the pointer, the result will be different.

Char* ss = "0123456789";

Sizeof (ss) result 4 = = "ss is a character pointer to a string constant, and sizeof gets the space occupied by a pointer, so it should be a long integer, so it is 4.

And the result of sizeof (* ss) 1 = = "* ss is the first character, which is actually the memory space occupied by the first bit'0' of the string, which is of char type, accounting for 1 bit.

Strlen (ss) = 10 > if you want to get the length of this string, be sure to use strlen. Sizeof returns the size of bytes occupied by the object. Strlen returns the number of characters.

3. A very special case when using sizeof is the metamorphosis of array names to pointers.

Char Array [3] = {'0'};

Sizeof (Array) = = 3;

Char * p = Array;

The result of strlen (p) = = 1 is 4.

When you pass an array name to a function, it is completely reduced to a pointer

4. Memory completion

Class X

{

Int i

Int j

Char k

}

X x

Cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report