The usage of sizeof function in cc++

Source: Internet
Author: User

The use of the sizeof () function in C + +


Learning C + + sometimes encounters the following scenario:


Known char *str1= "ABSDE";


Char str2[]= "ABSDE";


Char str3[8]={' a '};


Char STR4 [] = "0123456789";





Why sizeof (STR1) =4


sizeof (STR2) = 6;


sizeof (STR3) = 8;


sizeof (STR4) = 11;
Confused, let's discuss it together, and what's the matter with sizeof ().





Let's start by stating that the char type variable occupies one byte in memory, so sizeof (char) is 1,
We should have a good understanding of this.


STR1 is a pointer variable that simply points to the string "Absde".
sizeof (STR1) is neither the space length of the string, nor the space length of the character array,
Instead, a character-type pointer takes up space, so sizeof (STR1) =sizeof (char*) =4 (because a pointer occupies 4 bytes in C/s + +).





STR2 is an array of character types. C + + Specifies that for an array, the total space occupied by this array is returned,
So sizeof (STR2) Gets the total space occupied by the string "Absde".
"Absde", a total of a B s d e \ 06 characters,
So the length of the str2 array is 6, so sizeof (STR2) =6*sizeof (char) = 6.





STR3 has been defined as an array of length 8, so sizeof (STR3) is 8.





STR4 and str2 similar, ' 0 ' 1 ' ... ' 9 ' plus ' 11 ' characters, so STR4 occupies 11 of the space.





In short, for pointers, the sizeof operator returns the space that the pointer occupies, typically 4 bytes;


For an array, sizeof returns the total space occupied by all elements of this array.
char* and char[] easy to confuse, we must distinguish,
and the char*= "AAA" is now not advocated, should be avoided.





Strlen does not distinguish between an array or a pointer, and returns the length of the study until it is reached.
And strlen is not to count the length of the string.





To further understand the use of sizeof (), strlen (), and the difference between the two, you may wish to write a few small programs yourself and try to understand.

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.