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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software