Programmer interview 50 questions-sizeof usage (6), 50 sizeof
The following is a 32-bit C ++ program in Windows. Calculate the sizeof value.
Void Func (char str [1, 100])
{
Sizeof (str) =?
}
Void * p = malloc (100 );
Sizeof (p) =?
Answer:
Sizeof (str) = 4
Sizeof (p) = 4
Analysis:
When the array name in the Func (char str [100]) function is used as the function parameter, In the function body, the array name loses its meaning, only
It is only a pointer. Without its connotation, it also loses its constant feature and can perform auto-increment, auto-subtraction, and other operations.
Modify.
The essence of array names is as follows:
(1) The array name represents a data structure, which is an array;
For example:
Char str [10];
Cout <sizeof (str) <endl;
The output is 10. str indicates the data structure char [10].
(2) The array name can be converted to a pointer pointing to the object. It is a pointer constant and cannot be used for auto-increment, auto-subtraction, or other operations.
Can be modified;
Char str [10];
Str ++; // compilation error, prompting that str is not the left Value
(3) When the array name is used as a function parameter, it becomes a common pointer.
On Windows NT 32-bit platform, the pointer length (memory size occupied) is 4 bytes, so sizeof (str ),
Sizeof (p) is 4.