[Study Notes] [C Language] string array, learning notes String Array
1. application scenarios
* A one-dimensional character array stores a string, for example, a char name [20] = "mj"
* To store multiple strings, such as the names of all students in a class, a two-dimensional character array is required, char names [15] [20] can store the names of 15 students (assuming the name cannot exceed 20 characters)
* If you want to store the names of two students, you can use a three-dimensional character array char names [2] [15] [20].
2. Initialization
Char names [2] [10] = {'J', 'A', 'y', '\ 0'}, {'J',' I ', 'M', '\ 0 '}};
Char names2 [2] [10] = {"Jay" },{ "Jim "}};
Char names3 [2] [10] = {"Jay", "Jim "};
3. Code
1 # include <stdio. h> 2 3 int main () 4 {5 // char name [] = {'I', 't', 'C', 'h','s ', 'T', '\ 0'}; 6 char name [] = "itcast"; 7 8 name [3] = 'H '; 9 10/* 11 int size = sizeof (name); 12 13 printf ("% d \ n", size ); 14 */15 16 printf ("I'm in % s Class \ n", name); 17 18 return 0; 19} 20 21 // One of the strings initializes 22 void test2 () 23 {24 // the ASCII value of \ 0 is 025 // both are strings 26 char name [8] = "it"; 27 char name2 [8] = {'I ', 'T', '\ 0'}; 28 char name3 [8] = {' I ', 't', 0 }; 29 char name4 [8] = {'I', 't'}; 30 31 // not a single string (only one character array can be said) 32 char name5 [] = {'I', 't'}; 33} 34 35/* 36 void test () 37 {38 // 'A' B 'a' 39 // "jack" = 'J' + 'A' + 'C' + 'K' +' \ 0 '40 41 char name [10] = "jack888 \ n "; 42 43 // pass in the array, just a warning 44 printf (name); 45 46 printf (name); 47 48 printf (name); 49 50 printf ("57843578435 "); 51 }*/
Note:
1 # include <stdio. h> 2 3/* 4 \ 0 Function 5 1. string end mark 6 2. printf ("% s", name2); 7 will output characters starting from the name2 address until it reaches \ 0 8 */9 10 int main () 11 {12 char name [] = "itc \ 0ast"; 13 14 char name2 [] = {'O', 'K '}; 15 16 // printf ("% s \ n", name2); 17 18 printf ("% s \ n", & name2 [1]); 19 20 return 0; 21}