char* str = "abcd"; char str = "abcd";
For example, when allocating memory, what are the other differences
const char *str1 = "abcd";This statement allocates a block in memoryStatic memoryStore the character array, and then assign the first address of the character array to the pointer
str1It’s a pointer. Use
sizeofWhen an operator operates on it, it returns the size of the pointer.
char str2 = "abcd";This statement is inStack memoryThe first address of the character array is given to
str2, identify the array with
sizeofWhen the operator operates on it, it returns the array name
str2The number of elements of the identified array.
sizeof(type) sizeof expression
Both versions return one
size_tConstant of type.
1) return the size of the object corresponding to the type (in order tobyteIn units).
2) return the size of the object corresponding to the return type of the expression (withbyteIn units).
So the original answer is wrong. Now it is corrected as follows:
sizeof str1Pointer returned
str1Byte size of,
sizeof str2The return is
str2The byte size of all elements of the identified array.
“When the sizeof operator is used to operate on it, it returns the number of elements of the array identified by the array name STR2. “