"Learning note", "C language" string array

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.