For character arrays and character pointers:
1. if it appears as a string, the compiler will automatically add a 0 to the string as the Terminator. For example, if you write "ABC" in the Code, the compiler will help you store "ABC \ 0 ".
2. The direct string quantity is used as the initial value of the character pointer.
"Hello" is a string directly measured. The Compiler processes it as const char *, and the memory space associated with it is located in the read-only part of the memory, that is, the compiler is allowed to reuse references directly pointing to equivalent strings to optimize memory usage,
Even if the program uses a string for 500 times, the compiler only creates an instance in the memory. For example, char * PTR = "hello"; equivalent to const char * PTR = "hello ";
The string directly counts "hello" and is associated with the read-only memory. An error occurs if you try to modify the value. For example, PTR [1] = 'a'; may cause an error.
3. The number of strings directly serves as the initial value of the stack-based character array
Since stack-based variables cannot reference memory stored elsewhere, the compiler will be responsible for copying the string directly to the stack-based Array Memory.
For example, char stackarray [] = "hello ";
Make the following changes: stackarray [1] = 'a'; is true.
4. character array and character pointer
The character array is in the following format, and the characters are directly copied to the stack:
Char STR [] = "ABC"; // actual data storage: a B C \ 0, that is, a terminator \ 0 is added.
Char STR [3] = {'A', 'B', 'C'}; // the actual data storage: a B C, and no Terminator is added at the end.
Char STR [10] = {'A', 'B', 'C'}; // actual data storage: a B C \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0
The character pointer format is as follows:
Char * STR = "ABC"; // actual data storage: a B C \ 0, that is, a terminator \ 0 is added.
5. Determine the type
1). the array type is determined by the type of elements stored in the array and the size of the array.
For example, char S1 [3] and char S2 [4], S1 is Char [3], S2 is Char [4], that is, although S1 and S2 are character arrays, however, the two types are different.
2). The String constant type can be understood as the type of the corresponding character constant array.
For example, the "abcdef" type can be considered as const char [7], that is, the actual data storage is "abcdef \ 0 ".
3). In the function parameter list, parameters are written as arrays. The Compiler interprets them as common pointer types.
For example, for void func (char SA [100], int Ia [20], char * P), the SA type is char *, and IA type is int *, the p type is char *.
For 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.
4. the sizeof parameter of the array is not degraded. If it is passed to strlen, It is degraded to a pointer.
5. Most of the compilation programs calculate sizeof during compilation, which is the type or variable length. This is why sizeof (x) can be used to define the array dimension.
Char STR [20] = "0123456789"; // STR is an array of fixed size during compilation.
Int A = strlen (STR); // A = 10; // strlen () is determined at run time, and the actual length is calculated.
Int B = sizeof (STR); // B = 20; // sizeof () is determined during compilation. The STR type is int [20], the memory occupied is calculated.
6. The strlen result can be calculated only during running. It is used to calculate the actual length of the string, 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.
Char C;
Sizeof C; // variable names can be left blank
8. When it is applicable to a structure type or variable, 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.
Sizeof, strlen calculate character array, character pointer Space
Char STR [] = "ABC "; |
Actual data storage: a B C \ 0, that is, a terminator \ 0 is added. |
Its type is Char [4]. |
VS: sizeof (STR) = 4 strlen (STR) = 3 GCC: sizeof (STR) = 4 strlen (STR) = 3 |
Char STR [] = "ABC "; |
Actual data storage: a B C \ 0, that is, a terminator \ 0 is added. |
Its type is Char [4]. |
VS: sizeof (STR) = 4 strlen (STR) = 3 GCC: sizeof (STR) = 4 strlen (STR) = 3 |
Char STR [] = {'A', 'B', 'C '}; |
Actual data storage: a B C, does not add a terminator at the end |
Its type is Char [3]. |
VS: sizeof (STR) = 3 strlen (STR) = 15 GCC: sizeof (STR) = 3 strlen (STR) = 6 |
Char STR [3] = {'A', 'B', 'C '}; |
Actual data storage: a B C, does not add a terminator at the end |
Its type is Char [3]. |
VS: sizeof (STR) = 3 strlen (STR) = 15 GCC: sizeof (STR) = 3 strlen (STR) = 6 |
Char STR [5] = {'A', 'B', 'C', 'D', 'E '}; |
Actual data storage: a B c d e does not add a terminator at the end. |
Its type is Char [5]. |
VS: sizeof (STR) = 5 strlen (STR) = 19 GCC: sizeof (STR) = 5 strlen (STR) = 8 |
Char STR [5] = {'A', 'B', 'C', 'D '}; |
Actual data storage: a B c d \ 0 (default fill character \ 0) |
Its type is Char [5]. |
VS: sizeof (STR) = 5 strlen (STR) = 4 GCC: sizeof (STR) = 5 strlen (STR) = 4 |
Char * pstr = "ABCDE "; |
Actual data storage: a B c d e \ 0 |
The pstr type is char * |
Sizeof (pstr) = 4 (pointer data storage space, 4 bytes), strlen (pstr) = 5 |
Summary:
1). the sizeof result is the size of the type. After the sizeof is distinguished, the result of sizeof is killed. The result of sizeof is determined during the compilation period, and the memory occupied by the calculation.
The result of srelen is determined during running, and the actual length is calculated. strlen can only use char * as the parameter and \ 0 as the Terminator. In the above example, the strlen calculation in the red part is incorrect,
The strlen results seem to be a little abnormal because STR does not contain \ 0 characters in data storage.
2) When calculating sizeof:
Char STR [] = "ABC"; Type: Char [4], sizeof (STR) = 4 * sizeof (char) = 4.
3). sizeof (Express), in which express will not be compiled during compilation, but will be replaced.
For example: int A = 1; sizeof (A = 2 );
At this time, express is a = 2 and is replaced with sizeof (INT) during compilation. Therefore, after execution, A is still equal to 1.
4). When sizeof is used for a function, it will be replaced by the type of the return value of the function in the compilation phase.
For example, the result of int F () {return 0;} sizeof (f (); is 4.
Void F () {} sizeof (f (); an error occurs during compilation. The sizoeof (void) Compilation after the replacement cannot pass.