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