1. String
Strictly speaking, the C language does not have the type of string native string, but is implemented by a character pointer: char *p = "LINUX";. For other high-level languages, such as C + +, there is a string type: string p1 = "I love LINUX";.
A string in memory is actually composed of multiple bytes, and the address is contiguous; The pointer points to the head of the string, and the end of the string is a flag that ends with "\" (0 of the true can) as a string.
Understanding of "the":
The-S is an ASCII character that is actually encoded as a zero character and is really 0. This is different from the number 0, and the number zero ASCII is 48.
Attention:
When defining a string: char *p = "LINUX", the system will automatically add '+ ' at the end of the string as the end of the flag, so ' "" is the last character of the connection, but does not belong to this string.
The assigned string, which is saved to the literal constant area
int Main (intChar *argv[]) { char"I Love LINUX " ; printf ("%c\n", *p); printf ("%s\n", p); while (1);}
The output is:
II Love LINUX
2, character array
Definition:char"I Love LINUX";
In fact, the string is placed in the array, in a contiguous amount of memory in order to emit, the array name is the address of the first underworld to the string.
int Main (int argc, char *argv[]) { char p[] = " i love LINUX " ; for (int i = 0 ; I < 12 ; I++ " %c , * (P + i)); while (1 );}
Or you can directly printf ("%s\n", p);, output.
An array of characters is an array of the entire string at once, with the example above, which is actually a 12 element. Char p[12]={' i ', ' ', ' l ', ' O ', ' V ', ' E ', ' ', ' l ', ' I ', ' N ', ' U ', ' X '},12 elements are emitted sequentially.
Note: Because it is a string, the compiler will also automatically add \ ' 0to the end of the string. The assigned character array, which is assigned to the heap.
3. sizeof, strlen and string, character array
sizeof
is the C-language operator that calculates the size of the memory space occupied by the variable.
Strlen
is a function that calculates the length of a string. It's over.
Calculation:
Char str["0123456789"; int a =sizeof(str);
A = 10, because strlen () is a function that calculates the length of a string, and when it encounters a ' \ ' It ends, so the resulting value is 10.
b = 20, since the sizeof function calculates the size of the allocated memory space, it is obvious that the array is allocated 20 bytes, so the result is 20.
Char*STR1 ="ABCDE";CharStr2[] ="ABCDE";Charstr3[8]={'a',};CharStr4[] ="0123456789";sizeof(STR1);//4sizeof(STR2);//6sizeof(STR3);//8sizeof(STR4);// One
Analysis, it is first known that the sizeof function calculates the amount of allocated memory space. STR1 is obviously a pointer, no matter what, only accounted for the number of bytes, is always four bytes; str2 is a character array, although the letter is five, but it must be known that the letter is followed by an invisible ' "\" as the end of the string symbol, so it is a total of 6 characters, So it's 6 bytes; Str3 obviously has defined the number of the length is 8, so can only be eight bytes; Str4 is obviously also a character number, 10 characters plus a ' + ' string end flag, a total of 11 characters, so 11 bytes.
Strings and character arrays