Character array and string array

Source: Internet
Author: User
in C language: the definition of a character array and a string array

A character array is an array of characters

An array of strings is a special array of characters that ends with ' yes ' (ASCII is 0)

We cannot know how many members are in an array by character array names, but we can compute the number of members within an array by string array names

Here on the character array ( emphasis: The string array is a special character array ) The computation has a keyword sizeof, and a function strlen:

sizeof calculates the size of the space occupied by the character array (the number of members can be calculated by dividing the size of the space occupied by each member), but this number is a maximum number of members and may not actually be filled.

strlen To calculate the number of members with ' I ' as the end sign

So strlen can compute the number of members of the string array, but not the number of members of the character array.

Even if you calculate the correct result by using strlen to compute the normal character array member size, which is exactly 0 stored in the next memory address space at the end of the character array, you cannot use this method to calculate the number of members

And here the sizeof is used to compute the size of the space occupied by the character array, so the sizeof of the string array is ideally 1 more than the strlen (to hold ' the '), but there is also a lot of the other case, if you define char a[20 at the beginning] then it takes up 20BYTE , and Strlen is likely to be any number less than 20 (regardless of overflow) The character array is distinguished from the string array

When is a character array an array of strings, and when is an array of ordinary characters?

Note: under normal conditions, if the character array can hold more members than the number of members of the character array, it is automatically appended ' "" (it can be understood that all members of the character array before adding data are initialized to 0, regardless of overflow)

If you use scanf for%s input, you should enter a char * string, so even if it overflows, it will give you a ' yes ', so it's not safe.

Special, if using Char a[]={' A ', ' B ', ' C ', ' d ' is not going to automatically make up 0 here, so if you print an array of%s here, there will be garbled characters (the correct terminator cannot be found, but it can be output through%c one, but you must know the number of members). If you use char a[5]={' A ', ' B ', ' C ', ' d ' will automatically fill in 0, but be aware that the number of members must be less than the size of the array (at least one space to fill 0), otherwise the same will happen

In the same way we use a string to initialize a character array when using: Char a[5]= "ABCDE" is also wrong, although the length of the string is the number of members, but the string always ends with 0, so the size of the character array is more than 1 of the string length, If you use the compiler in this way, it should be a direct error (out of bounds). So it is convenient to use char a[]= "AAAAAA" to initialize the character array directly, sizeof (a) is 7,strlen (a) is 6

There is sizeof is the C language keyword, do not need to include header file, strlen need to include string.h header file

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.