How to do sizeof and strlen parsing

王林
Release: 2023-05-27 12:05:22
forward
1302 people have browsed it

1.strlenFunction.

What strlen does is just a counter, which starts from a certain location in memory ( It can be the beginning of the string, somewhere in the middle, or even an uncertain memory area) and starts scanning until it encounters the first string end character '\0', and then returns Counter value.

2.sizeofOperator

sizeof() returns the number of memory occupied after the variable is declared, It is not the actual length. In addition, sizeof is not a function, just an operator. The difference between

3.strlen and sizeof

3.1 sizeof can use type as a parameter, strlen can only use char* as a parameter, and it must end with ''\0''.

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

3.3 The parameters of array sizeof do not degenerate, and they degenerate into pointers when passed to strlen.

3.4 Most compilers calculate sizeof when compiling, which is the type or the length of the variable. This is why sizeof(x) can be used to define the dimension of an array.

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

3.6 After sizeof, if it is a type, brackets must be added. If it is a variable name, brackets may not be added. This is because sizeof is an operator and not a function.

4.Example difference between strlen and sizeof

1.char str[20]="0123456789";

int a=strlen(str); //a=10; >>>> strlen calculates the string Length, ends with the terminator 0x00 as the string.

int b=sizeof(str); //And b=20; >>>> sizeof calculates the size of the memory space occupied by the allocated array str[20], not Affected by changes in the content stored inside.

2.The above is the result of processing a static array. If it is a pointer, the result will be different

char* ss = "0123456789";  

sizeof(ss) Result 4 ===》ss is a character pointer pointing to a string constant. sizeof obtains the space occupied by a pointer, which should be It is a long integer, so it is 4

and the result of sizeof(*ss) is 1 ===》*ss is the first character. In fact, it is the first '0' of the string that is obtained. The memory space is of char type and occupies 1 bit

strlen(ss)= 10 >>>> If you want to obtain the length of this string, you must use strlen. The sizeof function returns the number of bytes occupied by its parameter in memory, while the strlen function returns the number of characters in the string pointed to by the parameter.

3.When using sizeof, there is a very special situation, that is, the array name is transformed into a pointer,

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

sizeof(Array) == 3;  

char *p = Array;  

strlen(p) = = 1; //sizeof(p) results in 4

When passing an array name to a function, it will completely degenerate into a pointer

4. Memory completion

class X

{

 int i;

 int j;

char k;

};

X x;

cout<

cout<

Memory completion is a knowledge point that is relatively easy to test in the written interview

The above is the detailed content of How to do sizeof and strlen parsing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template