From: http://blog.csdn.net/jphaoren/article/details/5803583
Example:
Char A [10];
1. assign values directly using strings during definition
Char A [10] = "hello ";
Note: you cannot assign a value to it first, as shown in figure
Char A [10];
A [10] = "hello ";
This is wrong!
2. assign values to characters in the array one by one
Char A [10] = {'h', 'E', 'l', 'l', 'O '};
3. Use strcpy
Char A [10];
Strcpy (a, "hello ");
Error prone:
1. Char A [10]; A [10] = "hello"; // how can a character hold a string? Besides, a [10] does not exist!
2. Char A [10]; A = "hello"; // This situation is prone. Although a is a pointer, it has pointed to the 10 character space allocated in the stack, now, in this case, A points to the hello constant in the Data zone. Here, pointer A is messy and not allowed!
Also, you cannot use the relational operator "=" to compare two strings. You can only use the strcmp () function for processing.
Operators in C language cannot operate strings at all. In C language, strings are treated as arrays. Therefore, the restrictions on strings are the same as those on arrays. In particular, they cannot be copied or compared using operators in C language. Directly copying or comparing strings will fail. For example, assume that str1 and str2 have the following statements:
Char str1 [10], str2 [10];
It is impossible to copy a string to a character array using the = Operator:
Str1 = "ABC";/***** wrong ***/
Str2 = str1;/*** wrong ***/
The C language interprets these statements as an (invalid) value assignment between a pointer and another pointer. However, using = to initialize the character array is legal:
Char str1 [10] = "ABC ";
This is because in the declaration, = is not a value assignment operator.
It is legal to try to use Relational operators or judgment operators to compare strings, but it will not produce the expected results:
If (str1 = str2).../*** wrong ***/
This statement compares str1 and str2 as pointers, rather than comparing the content of the two arrays. Because str1 and str2 have different addresses, the value of str1 = str2 must be 0.