1. Use Cases
* One-dimensional character array holds a string, such as a name char name[20] = "MJ"
* If you want to store multiple strings, such as the names of all students in a class, you need a two-dimensional character array, char names[15][20] can hold 15 students ' names (assuming the name does not exceed 20 characters)
* If you want to store two classes of student names, then you can use a three-dimensional character array Char names[2][15][20]
2. Initialization
Char Names[2][10] = {{' J ', ' a ', ' y ', ' \ n '}, {' J ', ' I ', ' m ', '/'}};
Char Names2[2][10] = {{"Jay"}, {"Jim"}};
Char names3[2][10] = {"Jay", "Jim"};
3. Code
1#include <stdio.h>2 3 intMain ()4 {5 //char name[] = {' I ', ' t ', ' C ', ' H ', ' s ', ' t ', ' n '};6 CharName[] ="Itcast";7 8name[3] ='H';9 Ten /* One int size = sizeof (name); A - printf ("%d\n", size); - */ the -printf"I'm at the%s class \", name); - - return 0; + } - + //an initialization of a string A voidtest2 () at { - //the ASCII code value of 0 is - //It's all strings . - Charname[8] ="it"; - Charname2[8] = {'I','T',' /'}; - Charname3[8] = {'I','T',0}; in Charname4[8] = {'I','T'}; - to //not a string (only an array of characters). + CharName5[] = {'I','T'}; - } the * /* $ void Test ()Panax Notoginseng { - //' A ' ' B ' a ' the //"Jack" = = ' J ' + ' a ' + ' C ' + ' K ' + ' + ' + A char name[10] = "jack888\n"; the + //Pass in the array, just a warning - printf (name); $ $ printf (name); - - printf (name); the - printf ("57843578435");Wuyi }*/
Attention
1#include <stdio.h>2 3 /*4 the role of5 1. End of string tag6 2.printf ("%s", name2);7 the character will be output from the address of name2 until it encounters a8 */9 Ten intMain () One { A CharName[] ="Itc\0ast"; - - CharName2[] = {'o','k'}; the - //printf ("%s\n", name2); - -printf"%s\n", &name2[1]); + - return 0; +}
"Learning note", "C language" string array