A strlen detail that is easily overlooked

Source: Internet
Author: User

Today, I saw a problem with the length of an array in csdn. I felt pretty good. I just summarized it and recorded it.

Char test [] = {0x01,0x02,0x03 };

Int A = strlen (test );

Int B = sizeof (test );

The A value is an uncertain value, because strlen determines that the string ends when the pointer goes to the address with the value '\ 000, '\ 000' indicates 0x00 in hexadecimal notation. The value B is 3, and sizeof is the memory occupied by the returned variables.
If chartest [] = {0x01,0x00,0x03}; then a = 1 because 0x00 is the string Terminator. B is still 3.

If chartest [20] = {0x01,0x02,0x03}; then a = 3 is initialized as 0x00 for the last 17 values. B is 20.

If chartest [20] = {0x01,0x00,0x03}; then a = 1. B = 20. The principle is as follows.

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.