Differences between string pointer variables and character Arrays
(1) allocate memory
The statement for defining character-type pointer variables and character arrays is as follows:
Char * PC, STR [100];
The system allocates 100 bytes of memory to STR, which is used to store 100 characters. The system only allocates four storage units for the pointer variable PC to store the address of one memory unit.
(2) Meaning of initialization assignment
The character array and the character pointer variable have the same initialization assignment form, but their meanings are different. For example:
Char STR [] = "I am a student! ", S [2, 200];
Char * Pc = "you are a student! ";
For character arrays, strings are placed in the storage space allocated to the array. For character-type pointer variables, strings are first stored in the memory, then, the memory start address of the string is sent to the pointer variable PC.
(3) Assignment Method
Character Arrays can only assign values to their elements one by one, but cannot assign strings to character array names. For character pointer variables, the string address can be directly assigned to the character pointer variable. For example:
STR = "I love China! "; // The STR character array name cannot be assigned directly. This statement is incorrect.
PC = "I love China! "; // The pointer variable PC can directly assign a string address. The statement is correct.
(4) Input Method
You can directly input strings into character arrays, rather than directly entering pointer variables. However, you can directly output the string indicated by the pointer variable.
Example: CIN> STR // correct
Cin> PC // Error
Cout <PC // correct
(5) value change
During program execution, the starting address indicated by the character array name cannot be changed, while the value of the pointer variable can be changed. Example: Str = STR + 5; // Error
PC = STR + 5; // correct
Summary character array s [100] pointer variable PC
(1) allocate memory to 100 units and allocate 4 units.
(2) assign value meaning put string into array bucket first store string to memory
Send the first address of the string to the PC.
(3) The assignment method can only assign values to each element. String addresses can be assigned to PCs.
(4) input mode: directly input character arrays to strings. pointer variables cannot be directly input to strings.
(5) value change: the first address of the character array cannot change the value of the pointer variable.
From the above differences, we can see that in some cases, it is easier to process strings with pointer variables than to process strings with arrays.
Both character arrays and character pointer variables can be used to store and operate strings. However, the two are different. Pay attention to the following issues during use:
1. The string pointer variable is a variable used to store the first address of a string. The string itself is stored in a contiguous memory space headed by this first address and ended with ''as the string. Character array is composed of several array elements, which can be used to store the entire string.
2. String pointer
Char * PS = "C Language ";
It can be written as follows:
Char * pS;
PS = "C Language ";
Array mode:
Static char st [] = {"C Language "};
It cannot be written as follows:
Char st [20];
St = {"C Language "};
You can only assign values to each element of the character array one by one.
From the above points, we can see the difference between the string pointer variable and the character array in use. It can also be seen that it is more convenient to use the pointer variable.
It is dangerous to use a pointer variable before obtaining a definite address, which may cause errors.
An error example is as follows:
Char * Name;
Scanf ("% s", name );
Printf ("% s", name );
Some compilers can also pass, but this is incorrect because it is a pointer that points to an unavailable address during definition. There are two ways to solve this problem: the array method or the method to allocate memory space to the character needle.
Array method:
Char name [20];
Scanf ("% s", name );
Printf ("% s", name );
How to allocate memory space to the character needle:
Char * Name;
Name = (char *) malloc (50); // The name already points to the allocated address space.
Scanf ("% s", name );
Printf ("% s", name );
However, you can assign values to pointer variables directly. This is because the C system must give a definite address when assigning values to pointer variables.
3. Int main ()
{
Char str1 [40] = "Hello world! "; // Char * str1 =" Hello world! ";
Str1 [4] = 'a'; // If str1 is pointer-type, It is compiled, but it will be an error when running
Printf ("% Sn", str1 );
Return 0;
} For "str1 [4] = 'a'; // If str1 is pointer-type, It is compiled, however, running the statement here may be a piece of error. The analysis of this sentence is very good, as follows: When str1 is a pointer type, "Hello world!" Is a String constant, put in memory, str1 points to its memory address.
What is a String constant? That is, we cannot change the content string. So str1 [4] = 'a'; in this way, an error occurs.
As for the reason for running errors rather than compiling errors, I think this is a problem with the compiler. I am using the GCC compiler. He may not know that str1 points to a String constant. Of course, we can design our own compiler to let it identify such errors so that the Program Reports errors during compilation.
Differences between string pointer variables and character Arrays