The difference and connection between sizeof and strlen

Source: Internet
Author: User

The difference and connection between sizeof and strlen
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 an operator 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

The difference and connection between sizeof and strlen

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.