Programmer interview 50 questions-sizeof usage (6), 50 sizeof

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.