1. sizeof is an operator and strlen is a function. The details are as follows:
Sizeof |
Strlen |
Header file: Stddef. h Format:1) sizeof (object); // sizeof (object ); 2) sizeof (type_name); // sizeof (type ); 3) sizeof object; // sizeof object FunctionMemory bytes occupied by an object or type; Description: The result type of the sizeof operator is size_t, typedef unsigned int size_t; |
Prototype: Extern unsigned int strlen (char * s ); Header file: String. h Format:Strlen (character array name) Function: Calculates the length of string S (unsigned int type), excluding '\ 0' Description: Returns the length of S, excluding the terminator null. |
2. strlen can only use char * as a parameter and must end with ''\ 0'', while sizeof is availableTypeParameters are also availableFunctionParameters, such:
Int sum ();
Printf ("% d \ n", sizeof (sum (); // The output result is sizeof (INT), that is, 4.
3. the sizeof parameter of the array is not degraded. If it is passed to strlen, It is degraded to a pointer.
4. in most compilation programs, sizeof is determined during compilation. Therefore, sizeof (x) can be used to define the array dimension. strlen can only be calculated at runtime to calculate the length of a string, instead of the memory size occupied by the type;
Char STR [20] = "0123456789 ";
Int len1 = strlen (STR); // len1 = 10;
Int len2 = sizeof (STR); // len2 = 20;
5. After sizeof, if it is of 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.
6. When applicable to a structure type or variable, sizeof returns the actual size. When applicable to a static space array, sizeof returns the size of all arrays.
The sizeof operator cannot return the size of the dynamically assigned array or external array;
7. 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 );
}
When sizeof and strlen are often used, it is usually used to calculate the length of the string array. If it is a pair of pointers, the results will be different:
Char * STR = "abacd ";
Sizeof (STR) // Result 4 ---> STR is a character pointer to a String constant. sizeof obtains the space occupied by a pointer. It should be a long integer, so it is 4;
Sizeof (* Str) // result 1 ---> * STR is the first character, which is actually the memory space occupied by the First 'A' of the string. It is of the char type, 1 place;
Strlen (STR) = 5 // ---> to obtain the length of this string, use strlen