In C language, when calculating the length of a character array, the principle of sizeof and strlen and the difference between the two

Source: Internet
Author: User
Tags array length

the length of the character array is calculated: You must use the Terminator '% ' as the boundary, but there are two ways to assign a value to a character array:

1: initialize with character when defined

(1) char chs[7] = {' A ', ' C ', ' 0 ', ' z ', ' 3 ', ' d '}; Length of 6

The upper formula is equivalent to: (2) char chs[7] = {' A ', ' C ', ' 0 ', ' z ', ' 3 ', ' d ', '/0 '}; Length of 6

Also equivalent to: (3) char chs[] = {' A ', ' C ', ' 0 ', ' z ', ' 3 ', ' d ', '/0 '}; Length of 6

But not equivalent to:
(4) char chs[] = {' A ', ' C ', ' 0 ', ' z ', ' 3 ', ' d '}; Unknown length because the array has no Terminator '/0 ' at the end

is not equivalent to:
(5) char chs[6] = {' A ', ' C ', ' 0 ', ' z ', ' 3 ', ' d '}//length unknown, because the array finally has no Terminator '/0 '

Where: (4) and (5) are also equivalent, but should not appear in the actual application, because no terminator array length is unknown, prone to array out of bounds, resulting in access to illegal memory, resulting in unexpected program errors.

Because character arrays are the C language concept, there is no guarantee in the C language syntax that the end of a character array must be terminator, which requires the programmer to take steps to ensure that the last element in the array is '/0 '.

2: Initialize a string array

There is no specific string variable in the C language, usually a character array to hold a string. The string always takes '/0 ' as the terminator of the string. So when a string is stored in an array, the Terminator '/0 ' is also stored in the array as a flag for whether the string ends. With the '/0 ' flag, it is not necessary to use the length of the character array to determine the length of the string.

The C language allows the array to be initialized with a string of values.

For example:

Char c[]={' C ', ' ', ' P ', ' r ', ' O ', ' g ', ' r ', ' A ', ' m '};

Can be written as:

Char c[]={"c Program"};

or remove {} write as:

Char c[]= "c program";

Assigning a value in a string is more than one byte per assignment of characters to hold the string end flag '/0 '. The actual storage of the above array C in memory is:

C P R o g R a m/0

'/0 ' is automatically added by the C compiler system. Since the '/0 ' flag is used, it is generally not necessary to specify the length of the array when assigning the initial value to the string, but the system handles it by itself.

3. The difference between sizeof and strlen

Strlen can only use char* to make arguments, and the char array must end with '/0 '.

The parameters of the array do sizeof do not degenerate, passed to the strlen is degraded to the pointer. For more information, see: Baidu knows: What is the difference between strlen and sizeof?

Http://zhidao.baidu.com/question/12033577.html

Note: Most compilers have computed sizeof at compile time, which is the length of the type or variable, which is why sizeof (x) can be used to define the dimensions of the array.

So, sizeof is able to calculate the "length" of the array even when it has no Terminator '/0 ', but the "length" here is actually the amount of memory allocated to the array variable by the compiler!

For example: char chs[] = {' A ', ' C ', '/0 ', ' z ', ' 3 ', ' d '}; sizeof (CHS) = 6; and strlen (CHS) = 2.

In C language, when calculating the length of a character array, the principle of sizeof and strlen and the difference between the two

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.