【學習筆記】【C語言】數組,學習筆記數組
1. 什麼是數組
數組,從字面上看,就是一組資料的意思,沒錯,數組就是用來儲存一組資料的
2. 數組的特點
只能存放一種類型的資料,比如int類型的數組、float類型的數組
裡面存放的資料稱為“元素”
3. 定義
聲明數組的類型
聲明數組的元素個數(需要多少儲存空間)
4. 格式
元素類型 數組名[元素個數];
比如:int ages[3];
5. 簡單使用
簡單初始化:int ages[5] = {19, 19, 20, 21, 25};
元素有順序之分,每個元素都有一個唯一的下標(索引),從0開始
數組元素的訪問:a[i]
6. 初始化
初始化方式
int a[3] = {10, 9, 6};
int a[3] = {10,9};
int a[] = {11, 7, 6};
int a[4] = {[1]=11,[0] = 7};
常見錯誤
int a[];
int[4] a;
int a[b];
a = {10, 11};
a[4] = {10,9,8,5};
7. 記憶體分析
數組儲存空間的大小
儲存空間的劃分(記憶體的分配是從高地址到低地址進行的,但一個數組內部元素又是從低到高進行的)
數組名的作用,查看元素地址
數組越界的注意
8. 其他使用
數組與函數參數
數組元素作為函數參數
數組作為函數參數(sizeof注意)
遍曆數組元素
兩種方式遍曆(while迴圈和for迴圈)
遍曆元素值和元素地址
字元數組的使用
9.代碼
1 #include <stdio.h> 2 3 int main() 4 { 5 // 使用注意 6 // 都是正確寫法 7 //int ages[5] = {10 , 11, 12, 67, 56}; 8 //int ages[5] = {10, 11}; 9 //int ages[5] = {[3] = 10, [4] = 11};10 //int ages[] = {10, 11, 14};11 12 // 錯誤寫法13 // int ages[];14 15 // 錯誤寫法16 /* 只能在定義數組的同時進行初始化17 int ages[5];18 ages = {10, 11, 12, 14};19 */20 21 // 正確寫法22 // int ages['A'-50] = {10, 11, 12, 14, 16};23 //int size = sizeof(ages);24 //printf("%d\n", size);25 26 // 正確寫法27 /*28 int count = 5;29 int ages[count];30 ages[0] = 10;31 ages[1] = 11;32 ages[2] = 18;33 */34 35 //printf();36 // 錯誤寫法37 // 如果想再定義數組的同事進行初始化,數組元素個數必須是常量,或者不寫38 //int ages[count] = {10, 11, 12};39 40 41 int ages[] = {10, 11, 12, 78};42 43 // 計算數組元素的個數44 int count = sizeof(ages)/sizeof(int);45 46 for (int i = 0; i<count; i++)47 {48 printf("ages[%d]=%d\n", i, ages[i]);49 }50 51 return 0;52 }53 54 // 數組的基本使用55 void arrayUse()56 {57 // 數組的定義格式: 類型 數組名[元素個數];58 int ages[5] = {19, 29, 28, 27, 26};59 // 19 19 28 27 26]60 ages[1] = 29;61 62 /*63 ages[0] = 19;64 ages[1] = 19;65 ages[2] = 28;66 ages[3] = 27;67 ages[4] = 26;68 */69 70 /*71 遍曆:按順序查看數組的每一個元素72 */73 for (int i = 0; i<5; i++)74 {75 printf("ages[%d]=%d\n", i, ages[i]);76 }77 }
數組的記憶體儲存細節
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 }30 31 32 void test1()33 {34 // 1.定義一個數組來儲存成績35 int scores[5];36 37 // 2.提示輸入成績38 printf("請輸入第1個學生的成績:\n");39 scanf("%d", &scores[0]);40 41 printf("請輸入第2個學生的成績:\n");42 scanf("%d", &scores[1]);43 44 printf("請輸入第3個學生的成績:\n");45 scanf("%d", &scores[2]);46 47 printf("請輸入第4個學生的成績:\n");48 scanf("%d", &scores[3]);49 50 printf("請輸入第5個學生的成績:\n");51 scanf("%d", &scores[4]);52 53 // 3.計算平均分,並且輸出54 int sum = 0;55 for (int i = 0 ; i<5; i++) {56 sum += scores[i];57 }58 printf("平均分是%f\n", sum/5.0);59 }60 61 void test()62 {63 /*64 char cs[5]= {'a', 'A', 'D', 'e', 'f'};65 66 printf("%p\n", cs);67 68 for (int i = 0; i<5; i++) {69 printf("cs[%d]的地址是:%p\n", i, &cs[i]);70 }*/71 72 int ages[3]= {10 , 19, 18};73 74 printf("%p\n", ages);75 76 for (int i = 0; i<3; i++) {77 printf("ages[%d]的地址是:%p\n", i, &ages[i]);78 }79 }
數組和函數
1 #include <stdio.h> 2 3 // 數組作為函數參數,可以省略元素個數 4 // 數組作為函數參數,傳遞是整個數組的地址,修改函數形參數組元素的值,會影響到外面的實參數組 5 6 void change(int array[]) 7 { 8 //printf("array==%p\n", array); 9 10 array[0] = 100;11 }12 13 void change2(int n)14 {15 n = 100;16 }17 18 int main()19 {20 int ages[6] = {10, 11, 10, 11, 10, 11};21 22 //printf("ages==%p\n", ages);23 24 change(ages);25 26 //change2(ages[0]);27 28 printf("%d\n", ages[0]);29 return 0;30 }
練習
1 /* 2 3 設計一個函數,找出整型數組元素的最大值 4 */ 5 6 #include <stdio.h> 7 8 int maxOfArray(int array[], int length) 9 {10 // 數組當做函數參數傳遞時,會當做指標變數來使用,指標變數在64bit編譯器環境下,佔據8個位元組11 12 //int size = sizeof(array);13 //printf("array=%d\n", size);14 15 //sizeof(array);16 17 // 1.定義一個變數儲存最大值(預設就是首元素)18 int max = array[0];19 20 // 2.遍曆所有元素,找出最大值21 for (int i = 1; i<length; i++)22 {23 // 如果當前元素大於max,就用當前元素覆蓋max24 if (array[i] > max)25 {26 max = array[i];27 }28 }29 30 return max;31 }32 33 int main()34 {35 int ages[] = {11, 90, 67, 150, 78, 60, 70, 89, 100};36 37 int ages2[] = {11, 90, 67, 150, 78, 60, 70, 89, 100};38 39 //int size = sizeof(ages);40 41 //printf("ages=%d\n", size);42 int max = maxOfArray(ages, sizeof(ages)/sizeof(int));43 44 printf("%d\n", max);45 return 0;46 }