1. The character pointer can point to a string.
We can initialize the character pointer with a string constant. For example, there is a description statement:
Char *str = "This is a string.";
is to initialize the character pointer. At this point, the character pointer points to the first address of a string constant, which points to the first address of the string.
Notice the difference between the character pointer and the character array. For example, there is a description statement:
Char string[]= "This is a string.";
At this point, string is an array of characters, which holds a string.
character Pointer Str with character array string The difference isthat STR is a variable that can change STR to point to different strings, but cannot change the string constants that Str refers to. A string is an array that changes the contents of the array to be saved.
2. Example:
Char *str, *str1= "This is another string.";
Char string[100]= "This is a string.";
In the program, you can use the following statement:
str++; /* pointer str plus 1 */
str = "This is a NEW string."; /* Point pointer to the new string constant */
str = STR1; /* Change the pointer to STR's POINT */
strcpy (String, "This is a NEW string.")/* Change the contents of the string */
strcat (String, str)/* For string connection operation */
In the program, you cannot do the following:
string++; /* Cannot perform + + operations on array names */
String = "This is a NEW string."; /* Wrong string operation */
string = str1; /* The array name cannot be assigned */
strcat (str, "This is a NEW string.")/* cannot be concatenated after STR */
strcpy (str, string)/* Cannot string copy to STR */
3 . Other instructions:
1) in the form of a string, the compiler will automatically add a 0 as the Terminator for the string, as in code: "ABC", then the compiler helps you to store "abc/0"
2) is "ABC" a constant? The answer is sometimes, sometimes not.
is not a constant : "ABC" as the initial value of the character array is not, as
Char str[] = "ABC";
Because the definition is a character array, so it is equivalent to define some space to hold "abc", and because the character array is to put the character one by one, so the compiler parses this statement to char str[3] = {' A ', ' B ', ' C '}, and according to the above summary 1, So char str[] = "abc"; The end result is char str[4] = {' A ', ' B ', ' C ', '/0 '};
To do an extension, if char str[] = "ABC"; it is written inside the function, then "abc/0" here is not a constant, so it should be placed on the stack.
is a constant condition: when assigning "abc" to a character pointer variable, such as
char* ptr = "abc";
Because the definition is a normal pointer, and does not define space to hold "abc", so the compiler has to help us find a place to put "ABC", obviously, the "ABC" here as a constant and put it in the program's constant area is the compiler's most appropriate choice. So although the type of PTR is not const char* and ptr[0] = ' x '; can also be compiled through, but execute ptr[0] = ' x '; a run-time exception occurs because this statement attempts to modify something in the program's constant area.
Remember which book has ever said char* ptr = "abc", which originally is not allowed in the C + + standard, but because this writing in C is too much, in order to compatible with C, not allowed to be allowed. Although allowed,
But the suggested wording should be const char* PTR = "abc"; So if the back is written ptr[0] = ' x ' The compiler will not let it compile, and it avoids the run-time exception mentioned above.
Again, if char* ptr = "abc"; written in the function body, then although the "abc/0" here is
is placed in a constant area, but PTR itself is just a normal pointer variable, so PTR is placed on the stack, but the object it points to is placed in the constant area.
3) The type of the array is determined by the type of things that the array holds and the size of the array itself. such as Char S1[3] and char s2[4],s1 type is CHAR[3],S2 type is char[4],
This means that although both S1 and S2 are character arrays, the two types are different.
4) The type of the string constant can be understood as the type of the corresponding character constant array,
such as the type of "abcdef" can be regarded as a const CHAR[7]
5) sizeof is used to find the number of bytes of the type. such as int A; then either sizeof (int) or sizeof (a) is equal to 4, because sizeof (a) is actually sizeof (type of a)
6) for formal parameters written in array type in the list of function arguments, the compiler interprets it as a normal pointer type, such as for void Func (char sa[100],int Ia[20],char *p)
The type of the SA is Char*,ia type int*,p is char*
7) According to the above summary, to combat a bit:
For char str[] = "abcdef", there is sizeof (str) = = 7, because the type of STR is char[7],
There are also sizeof ("abcdef") = = 7, because the type of "abcdef" is const CHAR[7].
for char *ptr = "abcdef", there is sizeof (ptr) = = 4, because the type of PTR is char*.
For char str2[10] = "abcdef", there is sizeof (str2) = = 10, because the STR2 type is char[10].
for void func (char sa[100],int ia[20],char *p);
There are sizeof (sa) = = sizeof (IA) = = sizeof (p) = = 4,
Because the type of the SA is Char*,ia, the type of int*,p is char*.
4. Difference:
(1) A character array consists of several elements , each of which holds one character of the string, while the character pointer variable holds the first address of the string.
(2) The initialization method is different. initialization of character arrays is done with static storage classes, at compile time. The initialization of a character pointer variable does not have to be static when it is actually executed.
(3) are assigned in different ways. the character array cannot be assigned as a whole and can only be converted to a portion of a single element. The character pointer variable assignment can be performed as a whole.
For example:
Char s[10];
s=/"c++/";/* wrong, S is constant, how can be assigned */
(4) when a character array is defined, the memory unit is allocated at compile time, with a definite address. when a character pointer variable is defined, the pointer variable is assigned a memory unit, but the pointer variable points to which string, and it is not known that the pointer variable holds an indeterminate address. For example:
Char a[10];
Char *p;
scanf (/"%s/", s);/* Correct */
scanf (/"%s/", p);/* Very dangerous, p value Dynamic */
(5) the value of the character pointer variable can be changed, and the character array name is a constant and cannot be changed. For example, there are simple programs:
Main ()
{
Char s=/"China man/"
s+=6;
printf (/"%s/", s);
}
Running Result: Man
character arrays and character pointers