7.3.1 Modify Content
In the example 7-3-1, the capacity of the character array A is 6 characters, and its content is hello\0. The contents of a can be changed, such as a[0]= ' X '. The pointer p points to the constant string "world" (in the static store, the content is world\0), and the contents of the constant string cannot be modified. Syntactically, the compiler does not find anything wrong with the statement p[0]= ' X ', but the statement attempts to modify the contents of the constant string and cause a run error.
Char a[] = "Hello";
A[0] = ' X ';
cout << a << Endl;
Char *p = "World"; Note that P points to a constant string
P[0] = ' X '; The compiler cannot find the error
cout << p << Endl;
Example 7-3-1 modifying the contents of arrays and pointers
7.3.2 Content Replication and comparison
You cannot copy and compare the array names directly. In the example 7-3-2, if you want to copy the contents of array A to array B, you cannot use statement B = A, or you will generate a compilation error. The standard library function strcpy should be used for replication. Similarly, the comparison between B and A is the same, not using if (b==a) to judge, should be compared with the standard library function strcmp.
The statement p = A does not copy the contents of a to the pointer p, but instead assigns the address of A to P. To copy the content of a, you can use the library function malloc for p to apply a strlen (a) + 1 characters of memory, and then use strcpy for string copying. Similarly, the statement if (P==a) compares not the content but the address, should use the library function strcmp to compare.
Array...
Char a[] = "Hello";
Char b[10];
strcpy (b, a); Can't use B = A;
if (strcmp (b, a) = = 0)//cannot be used if (b = = a)
...
Pointer...
int len = strlen (a);
Char *p = (char *) malloc (sizeof (char) * (len+1));
strcpy (P,a); Do not use P = A;
if (strcmp (p, a) = = 0)//Do not use if (p = = a)
...
Sample 7-3-2 and comparison of the contents of arrays and pointers