Character array initialization and assignment

Source: Internet
Author: User

Char a[10];
How do I assign a value to this array?
1, the definition of the time directly with a string assignment
Char a[10]= "Hello";
Note: You cannot define and assign a value to it first, such as Char a[10]; a[10]= "Hello"; this is wrong!
2. Assigning characters in an array to a value
Char a[10]={' h ', ' e ', ' l ', ' l ', ' o '};
3. Using strcpy
Char a[10]; strcpy (A, "Hello");

Error-prone situation:
1, Char a[10]; a[10]= "Hello";//How can a character hold a string? Moreover a[10] also does not exist!
2, Char a[10]; A= "Hello";//This situation is easy to appear, a although is a pointer, but it already points to the stack allocated 10 character space, now this case a again point to the data area of the Hello constant, here pointer a confusion, not allowed!

Also: You cannot use the relational operator "= =" to compare two strings, only with the strcmp () function.


The C-language operator simply cannot manipulate the string. string is treated as an array in the C language, so the strings are restricted in the same way as arrays, and in particular, they cannot be copied and compared using C-language operators.

Attempting to copy or compare a string directly fails. For example, assume that str1 and STR2 have the following declaration:

Char str1[10], str2[10];

It is not possible to use the = operator to copy a string into a character array:

STR1 = "ABC"; /*** wrong ***/

STR2 = str1; /*** wrong ***/

The C language interprets these statements as a (illegal) assignment operation between a pointer and another pointer. However, using the = Initialize character array is legal:

Char str1[10] = "ABC";

This is because in the declaration, = is not an assignment operator.

Trying to compare strings by using relational operators or operator-like operators is legal, but does not produce the expected results:

if (STR1==STR2) .../*** wrong ***/

This statement compares STR1 and str2 as pointers, rather than comparing the contents of two arrays. Because STR1 and str2 have different addresses, the value of the expression str1 = = Str2 must be 0

Character array initialization and assignment

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.