sizeof () and lstrlen () and strlen () differences

Source: Internet
Author: User

Strlen () is the byte length of the returned string,
Lstrlen () is the character length of the returned string.
This means that the second function may be the same as the first function result if the character units in the string are single-byte.

In general, the main difference is the ASCII character, or the difference between Unicode or UTF.
ASCII encoding is a character that occupies one byte, and Unicode is a character that occupies two bytes. To find the lengths of the two strings, use two functions to separate them.
Lstrlen (), strlen () is used to manipulate strings or character arrays, lstrlen (), strlen () to get the string length after the program runs
sizeof () can manipulate any class row
sizeof () computed by the compiler

Lstrlen used for Unicode
Strlen used for ANSI
sizeof compiler allocates memory size

Lstrlen
function function: This function returns the byte length of the specified string (ANSI version) or the character length (double-byte Standard Edition), which does not include the terminating null character.
Function prototype: int lstrlen (LPCTSTR lpstring);
Parameter: lpstring: A string that points to a null as a terminating character.
Return value: The length of the byte (ANSI version) or character (double-byte Standard Edition) of the specified string.

Strlen

Prototype: extern unsigned int strlen (char *s), in Visual C + + 6.0, the prototype is size_t strlen (const char *string), where size_t is actually a long integer.
Header file: string.h
Function: Calculates the length of the string s (unsigned int)
Description: Returns the length of S, excluding the Terminator null.

strlen (char*) function is the actual length of the string, it is calculated from the beginning to meet the first ' "", if you only define the initial value is not assigned to it, the result is uncertain, it will be from the AA header address has been looking down until the encounter ' "
Char Aa[10];cout<<strlen (AA) <<endl; The result is uncertain.
Char aa[10]={' "}; Cout<<strlen (AA) <<endl; Result is 0
Char aa[10]= "June"; Cout<<strlen (AA) <<endl; Result is 3
The sizeof () function returns the amount of memory the variable declaration takes, not the actual length, and sizeof is not a function, just an operator, and strlen is a function.
sizeof (AA) returns 10
int a[10]; sizeof (a) returns 40
The result type of the 1.sizeof operator is size_t, which is a typedef of type unsigned int in the header file.
This type guarantees that the byte size of the largest object being built can be accommodated.
2.sizeof is the operator (keyword) and strlen is a function.
3.sizeof can be used to type parameters, strlen can only use char* to do the parameters, and must be "" "to end.
sizeof can also use functions to make arguments, such as:
Short f ();
printf ("%d\n", sizeof (f ()));
The result of the output is sizeof (short), which is 2.
4. The parameters of the array do sizeof do not degenerate, passed to the strlen is reduced to a pointer.
5. Most compilers have computed sizeof at compile time, which is the length of the type or variable, which is why sizeof (x) can be used to define the number of dimensions of an array.
Char str[20]= "0123456789";
int A=strlen (str); a=10;
int b=sizeof (str); and b=20;
The results of 6.strlen are calculated at run time, and are used to calculate the length of the string, not the size of the memory.
7.sizeof after if the type must be parentheses, if the variable name can be non-parenthesized. This is because sizeof is an operator and not a function.
8. When applied to a struct type or variable, sizeof returns the actual size,
When applied to a static spatial array, sizeof returns the dimensions of all arrays.
The sizeof operator cannot return the dimensions of an array or an outer array that has been dynamically dispatched
9. The array is passed as a parameter to the function simultaneous is a pointer instead of an array, passing the first address of the array,
Such as:
Fun (char [8])
Fun (char [])
is equivalent to Fun (char *)
In C + +, the parameter passing array is always passed a pointer to the first element of the array, and the compiler does not know the size of the array
If you want to know the size of an array inside a function, you need to do this:
After entering the function, copy it with memcpy, and the length is passed in by another parameter.
Fun (unsiged char *p1, int len)
{
unsigned char* buf = new unsigned char[len+1]
memcpy (buf, p1, Len);
}
We can often use sizeof and strlen, usually to calculate the length of the string array
Read the above detailed explanation, found that the use of the two are still different, from this example can be seen very clearly:
Char str[20]= "0123456789";
int A=strlen (str); a=10; >>>> Strlen calculates the length of the string to end with the Terminator 0x00 for the string.
int b=sizeof (str); and b=20; >>>> sizeof calculates the size of the memory space allocated by the array str[20], which is not changed by the contents stored inside it.
Above is the result of the static array processing, if the pointer, the result is not the same
char* ss = "0123456789";
sizeof (ss) Result 4 = = = SS is a character pointer to a string constant, and sizeof obtains the space occupied by a pointer, which should be
Long integer, so it's 4.
sizeof (*SS) Results 1 = = = "*ss is the first character that actually gets the memory space occupied by the first bit ' 0 ' of the string, which is the Char class
The 1-bit
strlen (ss) = >>>> If you want to get the length of this string, be sure to use strlen
sizeof returns the size of the bytes occupied by the object. That's right
Strlen returns the number of characters. That's right
In the use of sizeof, there is a very special case, that is, the array name to the pointer transformation,
Char array[3] = {' 0 '};
sizeof (Array) = = 3;
char *p = Array;
sizeof (p) = = 1;//sizeof (p) result is 4
When passing an array name into a function, it is completely degraded to a pointer

Originated from http://blog.csdn.net/dongpanshan/article/details/7898574

sizeof () and lstrlen () and strlen () differences

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.