背景
此章笔记以读者有简单的C语言基础,对数组(array)、指针(pointer)有初步了解及应用,想扎实基础或深入探究为背景而写。
数组的使用
举个栗子:
- /*打印每月的天数(每四年错一次)*/
- #include <stdio.h>
- #define MONTHS 12
- int main(void)
- {
- const int days[MONTHS] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- int i;
- for(i = 0; i < MONTHS; i++)
- printf("Month %d has %2d days.\n", i+1, days);
- return 0;
- }
复制代码
输出如下:
* 用标识符常量表示数组大小,如果你觉得一年有13个月,只需修改#define语句
* 只读数组,初始化时加const
再举个栗子:
- /*打印每月的天数(让编辑器计算元素个数)*/
- #include <stdio.h>
- int main(void)
- {
- const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- int i;
- for(i = 0; i < sizeof days / sizeof days[0]; i++)
- printf("Month %d has %2d days.\n", i+1, days);
- return 0;
- }
复制代码
输出如下:
* 用空的方括号对数组进行初始化时,编译器会根据列表中的数值数目来确定数组大小
* 运算符sizeof给出其后的对象或类型的大小(以字节为单位)。因此sizeof days是整个数组的大小(字节),sizeof days[0]是一个元素的大小(字节)。相除就是数组中元素的数目。
指定初始化项目(C99)
- int arr[6] = {0,0,0,0,0,666}; //传统语法
- int arr[6] = {[5] = 666}; //C99新特性
二维数组
举个栗子:
- /*计算年降水总量、年降水平均量、月降水平均量*/
- #include <stdio.h>
- #define MONTHS 12
- #define YEARS 5
- int main (void)
- {
- const float rain[YEARS][MONTHS] = {
- { 1.5, 1.3, 1.2, 1.2, 1.3, 1.4, 1.3, 1.3, 1.4, 1.2, 1.3, 1.1 },
- { 1.2, 1.3, 1.4, 1.3, 1.8, 1.3, 1.1, 1.4, 1.2, 1.2, 1.3, 1.5 },
- { 1.3, 1.5, 1.3, 1.8, 1.3, 1.3, 1.3, 1.1, 1.4, 1.2, 1.4, 1.2 },
- { 1.2, 1.3, 1.5, 1.4, 1.4, 1.2, 1.3, 1.8, 1.3, 1.1, 1.3, 1.2 },
- { 1.1, 1.3, 1.4, 1.2, 1.3, 1.5, 1.1, 1.4, 1.2, 1.3, 1.8, 1.3 }
- };
- int year, month;
- float subtot, total;
- printf(" YEAR RAINFALL \n");
- for (year = 0, total = 0; year < YEARS; year++)
- {
- for (month = 0, subtot = 0; month < MONTHS; month++)
- subtot += rain[year][month];
- printf("%5d %15.1f\n", 2010 + year, subtot);
- total += subtot;
- }
- printf("\nThe yearly average is %.1f .\n\n", total / YEARS);
- printf("MONTHLY AVERAGE: \n\n");
- printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec\n");
- for (month = 0; month < MONTHS; month++)
- {
- for (year = 0, subtot = 0; year < YEARS; year++)
- subtot += rain[year][month];
- printf("%4.1f", subtot / YEARS);
- }
- printf("\n");
- return 0;
- }
复制代码
输出如下:( VS2015的框框丑得很 )
* 此程序有个缺点:会出现warning C4305“初始化: 从“double”到“const float”截断。
原因:浮点数默认为double型。此题可无视警告,不过最好在数据后面加f,标明是float型。 |