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 a parameter using a function, for example:
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 why sizeof (x) can be used to define the 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 used, 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, the pointer instead of an array is passed, 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 by parameters 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:
After entering the function, copy it with memcpy. 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 usually use sizeof and strlen to calculate the length of the string array.
After reading the detailed explanation above, we can see that there is a difference between the two. From this example, we can see clearly:
Char str [20] = "0123456789 ";
Int a = strlen (str); // a = 10; >>>> strlen calculates the length of the string, ending with 0x00 as the 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, which 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 first '0' character string and is a char class.
Type, occupies 1 place
Strlen (ss) = 10 >>> to obtain the length of this string, use strlen
This article is from the "fqzone" blog