Differences and relationships between sizeof and strlen)

Source: Internet
Author: User

1. The result type of the sizeof operator is size_t. In the header file, typedef is of the unsigned int type. This type ensures that it can accommodate the maximum object size.
2. sizeof is an operator and strlen is a function.
3. sizeof can be a type parameter. strlen can only be a char * parameter and must end with ''\ 0. Sizeof can also be used as parameters using functions, such as: Short F (); printf ("% d \ n", sizeof (f ())); the output result is sizeof (short), that is, 2.
4. the sizeof parameter of the array is not degraded. If it is passed to strlen, It is degraded to a pointer.
5. most compilation programs calculate sizeof as a type or variable length during compilation. This is sizeof (X) can be used to define the reason for array dimension char STR [20] = "0123456789"; int A = strlen (STR); // A = 10; int B = sizeof (STR ); // B = 20;
6. The strlen result can be calculated only during running. It is used to calculate the string length, not the memory size occupied by the type.
7. If sizeof is a type, you must add an arc. If it is a variable name, you can do not add an arc. This is because sizeof is an operator and not a function.
8. When a structure type or variable is applied, sizeof returns the actual size. When a static space array is applied, sizeof returns the size of all arrays. The sizeof operator cannot return the size of the dynamically assigned array or external array.
9. when an array is passed as a parameter to a function, it is a pointer rather than an array, and the first address of the array is passed, for example, fun (char [8]) Fun (char []) it is equivalent to fun (char *). In C ++, passing an array is always a pointer to the first element of the array, the compiler does not know the size of the array. If you want to know the size of the array in the function, you need to do this: Enter the function and copy it with memcpy, the length is passed into fun (unsiged char * P1, int Len) {unsigned char * Buf = new unsigned char [Len + 1] memcpy (BUF, P1, Len );}
When sizeof and strlen are often used, we usually calculate the length of the string array. After reading the detailed explanation above, we find that there is a difference between the two, we can see from this example:
Char STR [20] = "0123456789"; int A = strlen (STR); // A = 10; >>> strlen calculates the length of the string, end with the 0x00 ending character string. Int B = sizeof (STR); // while B = 20; >>> sizeof calculates the memory space occupied by the allocated array STR [20, the stored content is not changed.
The above is the result of processing the static array. If it is a pointer, the result will be different.
Char * Ss = "0123456789"; sizeof (SS) Result 4 = "SS is a character pointer to a String constant. sizeof obtains the space occupied by a pointer, it should be
Long Integer, so it is 4 sizeof (* ss) Result 1 = "* ss is the first character. In fact, it obtains the memory space occupied by the string's first '0, is a char class
Type, occupies 1 place
Strlen (SS) = 10 >>> to obtain the length of this string, use 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.