A glimpse of a character array

Source: Internet
Author: User
Tags array definition

What is a character array

Character arrays Save the array in the form of characters (essentially, the way a single character is saved with an array)

Array: initialized with int,float,double type

Character array: class with char type.

int i[]={1,2,3}; Char ch[]={'h','e','l ','l','o'};

The difference between a character array and a string

Our usual sense of string is a character set that is similar to "hello" and is enclosed in double quotation marks. The difference between the two is mainly focused on the definition and assignment.

There is no explicit division of strings in C, and a character array is typically used to initialize a string and assign a value. That

Char ch[]={'h','e','l' ,'l','o'}

If we're going to define a multidimensional character array, we need to do this.

Char ch[3][]={"hello","my", "  Love "}

It is obvious that such a way is too cumbersome in the definition assignment of multidimensional strings, and for this reason an alternative is presented in C + +, that is, to distinguish between the two by using the string type to refer specifically to strings. String differs from Char,char and int,float,double as the underlying type, has a fixed length in the program, and string belongs to the template type (Class), which in C # is referred to as a reference type and does not itself have a fixed length like char. Its length depends on the length of the quoted string in memory.

In the example above, we re-assign the string type as follows

string str="hello mylove"

Obviously, the readability of the program is enhanced compared to the previous example. There are a number of methods and properties for string that are easier to use, and will be explained in a later article, no more.

Character array Assignment

The assignment of a character array is performed directly after the character array definition, in the following ways

1 Charch1[]="Hello";2 Charch2[6]="Hello";3 Charch3[5]={'h','e','L','L','o'};4 Charch4[]={'h','e','L','L','o'};5 Char*ch5="Hello";//this assigns a value to the character pointer

Special Reminder: Array length should be string length +1 (end of string followed by)

Note: Character arrays cannot be directly assigned!!! Such as

Char ch[5];ch[5]= "Hello" is not legal

The difference between a character pointer and a character array

In contrast to the above assignment, the first four values are assigned to the character array, which is equivalent to opening up a number of memory spaces, each of which points to each memory space in turn. The fifth is the assignment of a character pointer, which is equivalent to opening up a number of spaces into a string and then pointing the character pointer to the first memory address. Because the character pointer points to a memory address, and the character array directly points to the contents in memory, the former supports direct assignment and the latter does not

Char *ch;ch="hello";      // this is legal. Char ch[5];ch="hello";     // it's not legal.

about ' + '

In the previous class, the teacher once said that the string (character array) ends with ' s ', which is explained in particular:

'/' is automatically populated by the program and has two functions: (1) for marking the end of the string; (2) for filling bits, as detailed below

1 Charch1[]="Hello";//Auto Fill in string ' \ '2 Charch2[6]="Hello";//equivalent to {' H ', ' e ', ' l ', ' l ', ' o ', ' n '}3 Charch3[5]={'h','e','L','L','o'};//five-bit characters are all occupied, no ' s '4 Charch4[7]={'h','e','L','L','o'};//There are two seats, filled with two ' + '5 Charch5[]={'h','e','L','L','o'};//dynamic allocation of memory, no '/'

Due to the fact that the program is running when the ' \ S ' Terminator does not exist in the character array, it is possible to overflow the memory and cause the program to break, so you need to pay attention when assigning the value

A glimpse of a character array

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.