【學習筆記】【C語言】字串數組,學習筆記字串數組

來源:互聯網
上載者:User

【學習筆記】【C語言】字串數組,學習筆記字串數組

1.使用場合
* 一維字元數組中存放一個字串,比如一個名字char name[20] = "mj"
* 如果要儲存多個字串,比如一個班所有學生的名字,則需要二維字元數組,char names[15][20]可以存放15個學生的姓名(假設姓名不超過20字元)
* 如果要儲存兩個班的學生姓名,那麼可以用三維字元數組char names[2][15][20]


2.初始化
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.代碼

 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("我在%s上課\n", name);17     18     return 0;19 }20 21 // 字串的一個初始化22 void test2()23 {24     // \0的ASCII碼值是025     // 都是字串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     // 不算是一個字串(只能說是一個字元數組)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     // 把數組傳入,僅僅是個警告44     printf(name);45     46     printf(name);47     48     printf(name);49     50     printf("57843578435");51 }*/

注意

 1 #include <stdio.h> 2  3 /* 4  \0的作用 5  1.字串結束的標記 6  2.printf("%s", name2);  7  會從name2這個地址開始輸出字元,直到遇到\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 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.