[Interview Questions] What is the difference between sizeof and strlen? sizeofstrlen
Example: constchar * p = "Hello World"; char p [] = "Hello World"; what is the difference between length and occupied space?
A: You can use strlen (p) to determine the length. sizeof cannot be used for the first memory space and sizeof can be used for the second memory.
Also: Second: strlen (p) = 11, sizeof (p) = 12
Trigger:
L sizeof
Sizeof (...) is an operator. In the header file, typedef is an unsigned int. Its value is calculated after compilation. The parameter can be an array, pointer, type, object, function, etc. Its function is to obtain the maximum object size that can be accommodated. Because it is calculated during compilation, sizeof cannot be used to return the size of the dynamically allocated memory space. In fact, sizeof is used to return the type and space occupied by objects, structures, or arrays statically allocated. The returned value has nothing to do with the content stored by objects, structures, and arrays. Specifically, when the parameters are as follows, the values returned by sizeof are as follows:
Array -- size of the array space allocated during compilation;
Pointer -- size of the space used to store the pointer (the address length of the pointer, which is a long integer and should be 4 );
Type -- the space occupied by this type;
Object-the actual space occupied by the object;
Function: the space occupied by the return type of the function. The return type of the function cannot be void.
L strlen
Strlen (...) is a function that can be computed only at runtime. The parameter must be a character pointer (char *). When the array name is passed in as a parameter, the array actually degrades to a pointer. Its function is to return the length of a string. The string may be defined by itself or randomly stored in the memory. The function is actually used to traverse from the first address representing the string until the end character is NULL. The returned length does not include NULL.
L example
Const char * p1 = "Hello World ";
Printf ("sizeof (p1): % d strlen (p1): % d \ n", sizeof (p1), strlen (p1 ));
Output result: sizeof (p1): 4 strlen (p1): 11
Char p2 [] = "Hello World ";
Printf ("sizeof (p2): % d strlen (p2): % d \ n", sizeof (p2), strlen (p2 ));
Output result: sizeof (p2): 12 strlen (p2): 11
Char p3 [10] = "Hello ";
Printf ("sizeof (p3): % d strlen (p3): % d \ n", sizeof (p3), strlen (p3 ));
Output result: sizeof (p3): 10 strlen (p3): 5
When sizeof returns a p3 array, the size of the array space allocated by the compiler does not care how much data is stored in it. Strlen only cares about the stored data content and does not care about the size and type of the space.
Char * parr = newchar [10];
Printf ("sizeof (parr): % d strlen (parr): % d sizeof (* parr): % d \ n", sizeof (parr), strlen (parr ), sizeof (* parr ));
Output result: sizeof (parr): 4 strlen (parr): 0 sizeof (* parr): 1
The first output is actually intended to calculate the size of the dynamic memory space pointed to by parr, but this is counterproductive. sizeof considers parr as a character pointer, therefore, the returned result is the space occupied by the pointer (the pointer is stored in a long integer, so it is 4). The second result 23 may be different each time, this depends on what is stored in parr (from parr [0] to know that the first NULL ends); the third result, because * parr represents the characters in the address space indicated by parr, the length is 1.
Sizeof (int): 4
Sizeof (char): 1
Sizeof (float): 4
Sizeof (double): 8
Double fun () {*******} sizeof (fun (): space occupied by the return type of the 8 Function
The following is a summary of the programmer's interview book:
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.
Charstr [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:
Charstr [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 a 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 of the char type, 1 place
Strlen (ss) = 10 >>> if you want to obtain this
What is the difference between sizeof and strlen in C language?
The strlen (char *) function is used to calculate the actual length of the string. The method is from the beginning to the First '\ 0'. If you only define that the initial value is not assigned to it, the result is not correct. It will be searched from the first address of aa until '\ 0' is stopped.
Char aa [10]; cout <strlen (aa) <endl; // The result is uncertain.
Char aa [10] = {'\ 0'}; cout <strlen (aa) <endl; // The result is 0.
Char aa [10] = "jun"; cout <strlen (aa) <endl; // The result is 3.
The sizeof () function returns the amount of memory occupied after variable declaration, not the actual length.
Sizeof (aa) returns 10
Int a [10]; sizeof (a) returns 40
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 applicable to a structure type or variable, sizeof returns the actual size. When applicable to static space arrays, 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 usually use sizeof and strlen to calculate the length of the string array.
I have read the full text of ......>
What is the difference between strlen and sizeof?
Differences and relationships between Sizeof and Strlen)
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, the string is obtained... the remaining full text>