I. Essential differences
Sizeof is essentially different from strlen. Sizeof is a type of single-object operator in C language, such as ++ and --. It is not a function. sizeof has two levels of priority, and has a higher priority than Level 3 operators such as // and %, the size of the storage space of the operands is given in bytes. Strlen is a function provided by the standard library of C language. Strlen calculates the length of a string.
Ii. Usage differences
1. sizeof
The operands of sizeof can be data types, functions, and variables. The expressions are used as follows:
(1) Data Type
Sizeof (type)
For example, we can use sizeof (int) to calculate the storage space of an int type data ). Note that when the sizeof operand is a data type, brackets are required. The value is the number of bytes in the storage space occupied by the data type.
(2) variables
Sizeof (variable name)
If int a is defined, you can use sizeof (a) to calculate the storage space occupied by the variable. The specific size depends on the type of.
Note: Because sizeof is an operator sizeof a or sizeof (. (No parentheses can be used). If the operand is an array name, the number of bytes occupied by the Array Memory is displayed. If the array name is used as a function parameter, it degrades to a pointer.
(3) Expression
Sizeof (expression)
Sizeof can evaluate an expression. The Compiler determines the size based on the final result type of the expression. Generally, it does not calculate the size of the expression. Example: sizeof (1 + 1.5)
(4) function call
Sizeof (function name ())
Sizeof can also be used to evaluate a function call. The result is the size of the function return type, and the function is not called. For example, the following function is defined:
Int myprint ()
{
Printf ("hello \ n ");
Return 0;
}
Int main ()
{
Printf ("% d", sizeof (mypaint ()));
Return 0;
}
The result only prints the sizeof value of the type returned by the function, and does not print hello, indicating that the function myprint is not called.
According to the C99 standard, functions, expressions of undetermined types, and bit-field members cannot be computed with sizeof values. That is, the following statements are incorrect:
For example, sizeof (myprint) (note that sizeof (myprint () is acceptable ))
Or sizeof a void return type function, such:
Void foo (){}
Sizeof (foo ());
And bit domain:
Struct S
{
Unsigned int f1: 1;
Unsigned int f2: 5;
Unsigned int f3: 12;
};
Sizeof (S. f1 );
2. strlen
Strlen is not widely used as sizeof. The strlen parameter must be a char * pointer. If strlen is used to calculate the data type strlen (int), it is wrong. The strlen calculation must depend on the '\ 0' character in the character sequence. strlen determines whether the Character Sequence ends by judging whether' \ 0' is encountered.
The calculation principle is similar to the following two statements.
While (* p! = '\ 0 ')
Length ++
Strlen usage: includes the following parameters:
(1) char * pointer
Strlen (pointer name)
If the parameter is a pointer, the length of the pointer pointing to the character sequence is calculated. (Use '\ 0' as the judgment flag) for example:
Define char * p = "hello world"; strlen (p) = 11, while sizeof (p) = 4. We can see that strlen calculates the length of the string to which the Pointer Points, while sizeof calculates the memory space occupied by the pointer.
(2) array
Strlen (array name)
If the parameter is an array, a pointer is actually passed, and strlen will process the array in the above pointer processing mode.
Let's look at the following example:
Char a [] = "hh ";
Strlen ();
Obviously, the result of strlen is 2. But what if the array is assigned a value like this?
Char a [] = {'h', 'H '};
Strlen ();
So what is the result of strlen (a) now? This number is not necessarily because strlen calculates the length of the string starting with address, as the previous assignment method assigns hh to the array in the form of a string, the string Terminator '\ 0' is assigned a value. Then, strlen checks that the terminator stops computing, the second method of complex value is to assign a value with no Terminator '\ 0' in the form of a single character. In this case, we can use sizeof to get the result, but strlen cannot find the Terminator, continues until the end is found. So this number is uncertain.
Author's "path to growth"