《C Primer Plus》讀書筆記——數組和指針(1)

背景

此章筆記以讀者有簡單的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[i]);
    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[i]);
    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型。

指針和數組

example == &example[0] //數組名是該數組首元素的地址

example + 2 == &example[2]    //相同的地址
* (example + 2 ) == example[2] //相同的值

* (example + 2) //example的第三個元素的值
* example + 2   //第一個元素的值加2
  • 指針前運用運算符*即可得到該指針所指向的對象的數值
  • 對指針加1,等價於對指針的值加上它指向的對象的字節大小

函數、指針和數組

舉個栗子

/*計算數組所有元素之和*/
int sum (int *ar, int n)
{
    int i;
    int total = 0;
    for(i = 0; i < n; i++)
        total += ar[i]; //ar[i]與*(ar + i)相同
    return total;
}
  • 當且僅當在函數原型或函數定義頭的場合中,可使用int *ar代替int ar[]:
    int sum (int ar[], int n);

  • 任何情況下,形式int *ar都表示ar是指向int的指針。int ar[]只有在聲明形式參量時可如此,但可提醒讀者ar不僅指向一個int數值,而且這個int是一個數組中的元素。

還有一種技巧,直接修改指針本身,使指針依次指向各個數組元素。

#include <stdio.h>
#define SIZE 10

int sum(int *start, int *end);

int main(void)
{
    int array[SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    long answer;

    answer = sum(array, array + SIZE);
    printf("The total number of array is %1d.\n",answer);
    return 0;
}

int sum(int *start, int *end)
{
    int total = 0;
    while (start < end)
    {
        total += *(start++);
    }
    return total;
}

輸出如下:
輸出如下:

聲明數組參量

  • 函數原型
int sum (int *ar, int n);
int sum (int *, int );
int sum (int ar[], int n);
int sum (int [], int );

函數原型允許省略名稱,所以上面4種原型等價。

  • 函數定義
int sum (int *ar, int n)
{
    //code
}

int sum (int ar[], int n)
{
    //code
}

指針操作

  • 賦值
    ptr1 = urn;
    ptr2 = &urn[2];

  • 求/取值
    *ptr1

  • 取指針地址
    &ptr1

  • 指針加減整數
    ptr2 + 4
    ptr2++
    ptr2 - 4
    --ptr2

  • 求差值
    通常是兩個指針指向同一個數組內不同元素,求其距離,單位是相應類型(如int)大小。
    ptr1 - ptr2

  • 比較
    ptr1 > ptr2

指針減指針得整數,
指針減整數得指針。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章