C + + talking about the difference between strlen and sizeof

Source: Internet
Author: User

Give me a chestnut first:

A string array followed by a string pointer, then using sizeof and strlen to see the output.

Char stra[] = "2017-7-19";

Char *strb = "23333";

cout <<sizeof (a) <<ends<<sizeof (b) <<endl;

Above output 10 4;

A string array length of 10, because it contains the Terminator ' \ n ';

b The length of the string pointer is 4, (32-bit machine pointer length is 4, the other type is the same, no matter how long the string to point)

If you want to test the last one is not, loop to the last one and make a judgment is not "."

cout <<strlen (a) <<ends<<strlen (b) <<endl; "Note that the strlen parameter here is the type of char"

Above output 9 5;

Strlen actually returns the length of the string, which is the length of the data saved in the original string array/string pointer.

C + + talking about the difference between strlen and sizeof

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.