標籤:style blog io ar color os sp for on
1.數組
數組,快速定義多個變數.
數組定義: 資料類型 數組名[數組元素的個數] = {值1, 值2, 值 3};
數組所佔儲存空間大小 = 數組元素個數 * 每個元素所佔的儲存空間大小.
%lu unsigned long 無符號的長整型. 無符號 >=0 sizeof 儲存大小值 //用來計算一個變數,類型,以及數組所佔儲存空間的大小. int a[5] = { 2, 7, 4, 3, 6 }; printf("%lu\n", sizeof(arr)); //5 int b[5] = { 2, 7, 4 }; printf("%lu\n", sizeof(arr)); //5 int c[5] = { 0 }; printf("%lu\n", sizeof(arr)); //5,因為定義的空間大小為5 int d[] = { 2, 7, 4, 3, 6 }; printf("%lu\n", sizeof(arr)); //5
</pre><pre>
擷取數組中的元素:通過數組名 + 下標. c語言不會檢測數組下標越界.
arr[5] = 20; printf("%d",arr[5]);//角標越界
1.字串所佔的空間?少要比字串?度大1,因為字串以‘\0’表?示結束。系統提供的字串處理函數都是根據‘\0’來判斷字串是否結束。
char arr[] = "ipho"; printf("%lu\n", sizeof(arr)); //儲存空間大小為 5,因為有預設的\0 <pre name="code" class="cpp"><pre name="code" class="cpp"> printf("%lu\n", strlen(arr)); //字串長度為 4
例子:
1.講數組中的每一個元素輸出
int arr[5] = {1, 3, 5, 6, 7}; for (int i = 0; i < 5 ; i++) { printf("%d\n", arr[i]); }
2.給數組中的每一個元素賦值 [20, 40]
int a[10] = {0}; for (int i = 0 ; i < 10; i++) { a[i] = arc4random() % (40 - 20 + 1) + 20; printf("a[%d] is %d\n", i, a[i]); }
3.求數組中的最小值,給數組中的每一個元素賦值 [20 , 40].
int a[10] ={0};</span> <span style="color:#000000;">for (int i = 0 ; i < 10; i++) { a[i] = arc4random() % (40 - 20 + 1) + 20; printf("a[%d] is %d\n", i, a[i]); } int min = 0; for (int i = 0; i < 10; i++) { if (i == 0) { min =a[0]; } else { if (min > a[i]) { min = a[i]; } } } printf("最大值為:%d\n", min);
4.定義 一個10位元組,數組中的元素取值範圍為[30,50],求所有元素的和.
int arr[10] = {0}; for (int i = 0 ; i < 10 ; i++) { arr[i] = arc4random() % ( 50 - 30 + 1 ) + 30; printf("%d\n",arr[i]); } int sum = 0; for (int i = 0; i < 10; i++) { sum += arr[i]; } printf("所有元素之和為:%d\n", sum);
2.數組排序
冒泡排序:
涉及到雙迴圈:
外層迴圈控制趟數,外層減一, 可減可不減,-1目的是提供者的執行效率.
內層迴圈控制比較次數. 內層減一,必須減,防止比較時越界.
內層 - i ,可以不減, -i目的是縮小無序區的範圍,提高程式的執行效率.
代碼如下:
int a[10] = {0}; for (int i = 0; i < 10; i++) { a[i] = arc4random() % (15 - 10 + 1 ) + 10; printf("%d ", a[i]); } for (int i = 0; i < 9; i++) { for (int j = 0; j < 10 - 1 - i ; j++) { if (a[j] < a[j+1]) { //降序排列 int temp =a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } printf("\n"); for (int i = 0; i < 10 ; i++) { printf("%d " ,a[i]); }
3.字元數組
char str[4]= {‘F‘, ‘R‘,‘‘,‘L‘,‘N‘};
printf("%lu\n",sizeof(str5));
char str[] = "phone"; //字串
字串隱藏字元 \0,作為字串結束標誌.
如果不指定元素個數,則系統開闢空間,會自動識別初始值的元素個數開闢空間.
char str5[]= {'F', 'R', 'L', 'N','d'};//如果定義空間大小[6],sizeof為6,長度為5 char str6[]="frln"; printf("str5 = %lu\n",sizeof(str5)); //空間大小為5 printf("str6 = %lu\n",sizeof(str6)); // 空間大小為5,隱藏\0 printf("str5 =%lu\n",strlen(str5)); // 長度5 printf("str33 =%s\n",str5); // FRLND printf("str44 =%s\n",str6); // frln printf("str6 =%lu\n",strlen(str6)); // 長度4 如何輸出字串?
數組名:能夠代表數組的首地址,是一個常量.不能改變,不能賦值.
char str[] = "iphone";
printf("%s\n", &str[4]);//取出 ne,從第四個開始,取出以後的內容
printf("%c\n",str[4]);
3.字串處理
char str[10] = "language"; //字串的長度. printf("%lu\n", strlen(str)); //8 //字串拷貝 strcpy(str ,"aa"); printf("%s\n",str); //因為有隱藏字元 \0,所以只能列印出aa. //字串 拼接 strcat(str,"bb"); printf("%s\n",str);//因為有隱藏字元 \0,所以只能列印出aabb.但是後面還存在值,如下 printf("%c\n",str[6]); // 結果為g //字串比較 printf("%d\n",strcmp("aa","bb")); // b - a = -1 printf("%d\n",strcmp("da","bb")); // (d -b) + (b - a ) =2 printf("%d\n",strcmp("bba","bb")); //如上 97
【學習ios之路:C語言】一維數組,數組排序,字元數組