黑馬程式員——C語言——數組

來源:互聯網
上載者:User

標籤:

一、一維數組

      存放一組同種類型的資料。

      1、數組的定義  

        元素類型 數組名[元素個數];

       

1      //都是正確寫法2     int ages[5] = {10 , 11, 12, 67, 56};3     int ages[5] = {10, 11};4     int ages[5] = {[3] = 10, [4] = 11};5     int ages[] = {10, 11, 14};6     7     // 錯誤寫法8      int ages[];

 

      2、 數組的初始化        

     

1 // 錯誤寫法2     //只能在定義數組的同時進行初始化3     int ages[5];4     ages = {10, 11, 12, 14};
//正確寫法//只能在定義數組的同時進行初始化    int ages[5] = {10, 11, 12, 14};
1 // 正確寫法2     /*3     int count = 5;4     int ages[count];5     ages[0] = 10;6     ages[1] = 11;7     ages[2] = 18;8     */

    3、 數組的基本使用

 1 / 數組的基本使用 2 void arrayUse() 3 { 4     // 數組的定義格式: 類型 數組名[元素個數]; 5     int ages[5] = {19, 29, 28, 27, 26}; 6     // 19 19 28 27 26] 7     ages[1] = 29; 8      9     /*10      ages[0] = 19;11      ages[1] = 19;12      ages[2] = 28;13      ages[3] = 27;14      ages[4] = 26;15      */16     17     /*18      遍曆:按順序查看數組的每一個元素19      */20     for (int i = 0; i<5; i++)21     {22         printf("ages[%d]=%d\n", i, ages[i]);23     }24 }

    4、 數組在記憶體中的分析

       ①  數組儲存空間的大小 

       ②  儲存空間的劃分(記憶體的分配是從高地址到低地址進行的,但一個數組內部元素又是從低到高進行的)

       ③  數組名的作用,查看元素地址

   

 1 #include <stdio.h> 2  3 /* 4  提示使用者輸入5個學生的成績,算出平均分並且輸出 5  */ 6  7 int main() 8 { 9     10     11     // 1.定義一個數組來儲存成績12     int scores[5];13     14     // 2.提示輸入成績15     // 用來儲存總分16     int sum = 0;17     for (int i = 0; i<5; i++) {18         // 2.1 提示輸入某個學生的成績19         printf("請輸入第%d個學生的成績:\n", i + 1);20         // 2.2 儲存當前學生的成績21         scanf("%d", &scores[i]);22         // 2.3 累加成績23         sum += scores[i];24     }25     26     // 3.計算平均分,並且輸出27     printf("平均分是%f\n", sum/5.0);28     return 0;29 }

5、 數組與函數的關係和應用

    ① 數組作為函數參數,可以省略元素個數

    ②  數組作為函數參數,傳遞是整個數組的地址,修改函數形參數組元素的值,會影響到外面的實參數組

   

 1 #include <stdio.h> 2 void change(int array[]) 3 { 4     //printf("array==%p\n", array); 5      6     array[0] = 100; 7 } 8  9 void change2(int n)10 {11     n = 100;12 }13 14 int main()15 {16     int ages[6] = {10, 11, 10, 11, 10, 11};17     18     //printf("ages==%p\n", ages);19     20     change(ages);21     22     //change2(ages[0]);23     24     printf("%d\n", ages[0]);25     return 0;26 }

二、二維數組

     二維數組是一個特殊的一維數組:它的元素是一維數組

    1、數組的定義和初始化

        

1   int a[3][4] = {1,2,3,4,5,6};2   int a[3][4] = {{},{},{}};3 //數組元素簡單訪問4   int a[][5] = {3,21,31,2,32,1};5 6 //注意錯誤:7   int a[3][4];8   a[3] = {};

   2、二維數組的簡單運用

    

 1 /* 2      1 淺藍色 3      2 深藍色 4      3 黃色 5      4 紅色 6      -1 沒有 7      */ 8      9     int cubes[5][5] = {10         {1, -1, -1, -1, -1},11         {1, 1, -1, 2, -1},12         {4, 1, 2, 2, 2},13         {4, 3, 3, 3, 3},14         {4, 3, 3, 3, 3}15     };16     

 

黑馬程式員——C語言——數組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.