Strlen and sizeof

Source: Internet
Author: User

【1"What is the difference between strlen and sizeof?" (1The result type of the sizeof operator is size_t, which is a typedef of unsigned in the header fileinttype. This type guarantees that the byte size of the largest object being built can be accommodated. (2sizeof is an operator and strlen is a function. (3sizeof can use the type parameter, strlen can only use char* to do parameters, and must be"'\0"'end of. (4the array name does not degrade the parameters of sizeof, which is passed to the strlen as a pointer. (5most compilers have calculated sizeof at compile time. The length of a type or variable this is why sizeof (x) can be used to define the number of dimensions of an array. The sample code is as follows:1 Charstr[ -]="0123456789";//STR is an array that has a fixed size at compile time2 intA = strlen (str);//A=10;strlen () determines at run time3 intb =sizeof(str);//and B=20;sizeof () determines at compile time(6the results of the strlen are calculated at run time, and are used to calculate the actual length of the string, not the size of the type in memory. (7)sizeofIf the type must be bracketed, the variable name can be non-parenthesized. This is because sizeof is an operator and not a function. Example code:1 CharC;2 sizeofC//variable names can be non-parenthesized(8When applied to a struct type or variable,sizeofReturns the actual size, when applied to a static space array,sizeofreturns the dimensions of all arrays. sizeofThe operator cannot return the dimensions of an array or an outer array that has been dynamically dispatched. (9The 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[]) are 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. We often use thesizeofand strlen, the length of the string array is usually computed. Read the above detailed explanation, found that the use of the two are still different, from this example can be seen very clearly: the following:1 Charstr[ One]="0123456789";//Note that the str size is greater than or equal to 11, you should consider ' "", or the compiler will error2 intA=strlen (str);//a=10; strlen computes the length of the string, ending with a terminator 0x00 for the string. 3 intb=sizeof(str);//and b=11, sizeof calculates the size of the allocated array str[11], which is not changed by the contents of the stored content. above is the result of processing the static array, and if it is a pointer, the result will be different. Take a look at the following example:1 Char* ss ="0123456789";2 sizeof(ss)//4 ==ss is a character pointer to a string constant, and sizeof obtains the space occupied by a pointer, which should be a long integer, so it is 43 sizeof(*SS)//1 ==*ss is the first character that actually gets the memory space occupied by the first bit of the string ' 0 ', which is of type char, which accounts for 1 bits.4strlen (SS)//10 = = To get the length of this string, be sure to use strlen. In addition, the following method can be used to determine the number of elements that the static array can hold:1 inta[3]={1,2,3};2cout <<sizeofA/sizeof(typeID (a[0]). Name ());//3

3 int a[6]={1,2,3};
4 cout << sizeof a/sizeof (int);//6

Reprinted from: http://www.cnblogs.com/Braveliu/archive/2013/01/01/2841521.html

Strlen and sizeof

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.