Small issues worth attention in sizeof ()

Source: Internet
Author: User
 

Char A [] = {'s ', 'F', 'G', 'D', 'H '};

Char * B = "sfgdh ";

Char C [] = "sfgdh ";

Int a1 = 'a ';

Char d [6] = {'s ', 'F', 'G', 'D', 'H '};

Printf ("% d \ n", sizeof ());

Printf ("% d \ n", sizeof (B ));

Printf ("% d \ n", sizeof (c ));

Printf ("% d", sizeof (A1 ));

Printf ("% d", sizeof (d ));

Result:

5

4

6

4

6

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------

Sizeof is the number of bytes occupied by the object in the memory.

Sizeof (a): calculates the total number of bytes in array A. The total number of bytes must be determined by the array type and the number of array elements. The number of elements is required. sizeof (a)/sizeof (char) indicates the number of elements. Because char occupies one byte, the result is: 5. Remember that the array name is the first address of the first element of the array. It is a constant and a cannot be used for value assignment.

Sizeof (B): B is a pointer variable, that is, the variable that stores the address. No matter what type of pointer variable, sizeof () is the memory size occupied by the address, it is 4 bytes.

The array element in the C array is 6, because the last one is '\ 0 '.

The size of the space required by the object A1 obtained by sizeof (A1) in the memory. The Int type requires 4 bytes, so it is 4.

Sizeof (d) is a description. The number of arrays is determined by the value given by brackets. If not, it is determined by the number of braces.

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.