Source: http://hanxuedog.iteye.com/blog/1299186
There are several forms of defining a string in C: String constants, char arrays, char pointer 1. String constants
That is, any character that is in a pair of double brackets. Double quotes in the character plus the compiler automatically provides the end of the flag-character, as
A string is stored in memory. such as: printf ("%s", "Hello"); "Hello"
If there is no interval or interval between string literals, ANSI C concatenates them spaces. Cases:
Char greeting[50] = "Hello,and" "How Are" "You";
Equivalent to:
Char greeting[50] = "Hello,and How to Are You";
string constants belong to static storage classes. Static storage means that if you use string constants in a function, even if you call the function multiple times,
This string is stored only one copy throughout the program's run. The contents of the entire quotation mark as a pointer to where the string is stored. This point with
The array name is similar to the pointer to the location where the array is stored.
2. Array of strings and their initialization
Initialization example:
Char m[40] = "Hello,world"; When defining an array of strings, you must specify the size of the array (integer constant), and when you specify the size, make sure that the size of the array is larger than the scheduled one, because the compiler automatically adds '.
The extra elements are initialized to ' the '
Char m={' h ', ' e ', ' l ', ' d '}; Notice the null character at the end of the flag, and without it, you get just an array of characters instead of a string
3. Define a string using the char pointer
Char *m = "Hello,world"; Automatically add ' i '
Note: At this point the string pointer m points to the string constant, and no * (m+1) = ' O ' modifies this constant because the string constant is placed in the constant area and cannot be modified
4. Arrays and pointers
What is the difference between an array form and a pointer form?
Array form: char m[40] = "Hello,world";
Pointer form: char *m = "Hello,world";
Array form:
The compiler will treat the array name m as a synonym for the address &m[0 of the first element of the array, and M is an address constant. You can use M+1 to identify the next element in an array, but you cannot use ++m, and the increment operator can be used only before a variable, not before a constant.
M[40] is assigned a 40-element array in the computer's memory (where each element corresponds to one character, and an additional element corresponds to the end of the null character ' "). Each element is initialized to the corresponding character.
Typically, the quoted string is stored in the data section of the executable file, and when the program is loaded into memory, the string is loaded into memory, and the referenced string is copied into the array
Pointer form:
The pointer form (*M) also reserves space for the string in the static store. In addition, once the program starts executing, a separate storage location is reserved for the pointer variable m, so that the address of the string can be stored in the pointer variable.
M points to the first character of the string and can be ++m to the second character. Pointer m is a variable.