# Array

變數可以幫我們存取一筆資料,但要存取多筆資料時就會顯得很麻煩。

比如我們要記錄三個學生的成績:

# Declare

Data type  變數名稱[陣列大小]  =  {資料};

# Array Input/Output

# Two-dimensional Arrays

二維陣列

陣列裡面存放陣列

# Declare

Data type  變數名稱[column][row]  =  <!--swig1-->;

# Two-dimensional Arrays Input/Output

# String

字串

在 C programming language 中,沒有 string 的變數型態,
因此我們這邊提到的字串是以字元陣列的型態表示,
另一種是用 pointer 的型態來表示。

# Declare

char  變數名稱[陣列大小]  =  {‘資’, ‘料’};

char  變數名稱[陣列大小]  =  “資料”;

# Gets/Fgets/Puts

字串輸入 / 輸出

因為 gets 函式無法知道字元陣列的大小,而是依賴換行符號或 EOF 才會結束輸入,因此有可能引發緩衝區溢位的安全問題 。

# Scanf

# String of function

There are many important string functions defined in "string.h" library.

# Strlen

The strlen() function returns the length of the given string.
It doesn't count null character '\0' .

# Strcpy

The strcpy(destination, source) function copies the source string in destination.

# Strcat

The strcat(first_string, second_string) function concatenates two strings and result is returned to first_string.

# Strcmp

The strcmp(first_string, second_string) function compares two string and returns 0 if both strings are equal.

# Strrev

The strrev(string) function returns reverse of the given string.

# Strlwr

The strlwr(string) function returns string characters in lowercase.

# Strupr

The strupr(string) function returns string characters in uppercase.

# Strstr

strstr () 函數返回指向給定字符串中第一次出現的匹配字符串的指針。
它用於返回從第一個匹配到最後一個字符的子字符串。

# Function

函式

A function is a block of code which only runs when it is called.
Functions are used to perform certain actions, and they are important for reusing code: Define the code once and use it many times.

我們可以簡單的將程式的函數分成兩種:

  • 有回傳值的
  • 無回傳值的

# Advantage

函式 (function) 是程序抽象化 (procedure abstraction) 的實踐方法:

  • Reusability 重複利用性
  • Readability 易讀性
  • Reduce Coupling 降低耦合性 (耦合,指的是和其它程式碼「有」相關)
  • Modularity of the program 模組化

# Syntax _ return value

# Syntax _ without parameter

# Syntax _ without return value

# Function

Function 可以把他想成某個功能的實現,用於簡化 main 中的程式,並妥善的將程式清楚區分功能,達到重用、降低耦合性,也讓寫思考程式功能更能分塊撰寫。

在這邊我們可以先不管 Function 內的程式是如何寫的,但是我們知道他可以實現他的功能;區分好後再開始分段思考,要如何撰寫 Function 內的程式可以達到我們所希望的樣子。

# Pointer

# Recall

Data type  變數名稱  =  資料;

# Value

# Pointer

# Declare

  • T* ptr
    ptr is pointed a T type object/value

  • &value
    get address of value

  • *ptr
    Access data by an address

# Pointer

# Pointer in Pointer

# Pointer and Array

# Thinking

請問右邊程式執行後會輸出什麼呢?

# Struct

結構 (structure) 是一種複合型別 (derived data type),用來表達由多個屬性組成的型別,而這些屬性可以是基本型別或是另一個複合型別所組成。

# 宣告結構

# 存取結構內屬性

# 內嵌在結構內的結構

# 儲存結構的陣列

# 存取結構指標的屬性

# Scope

主要是指我們的變數都有可以使用的範圍,通常一個可以用的範圍是其 “{} ” 內。

EX: 自訂 function 和 main function,其實是兩個不同括號 (“ {} ” ) 的兩段程式碼,所以它裡面的變數命名一樣是不會導致錯誤的,因為對程式來說它是兩塊不同的東西。

EX: main function 中 while/for 中所定義的變數,在 while/for 的 “{} ” 是無法被使用的。

# Argument Passing

# Call by Value

利用 call by value 的方法去傳值時,因為 a, b 的記憶體和 swap function 內 a, b 的記憶體是分開的,所以不會互相干擾,但需要使用另外的兩個記憶體去儲存他們!

# Call by Address

使用了一個 swap function,它的功能是將 a 和 b 傳入的 address 進行交換,之後在 main 裡面利用 & 符號傳入 address ,然後執行一遍就可以發現,交換前 a 的值為 1,b 的值為 0,交換後 a 的記憶體空間和 b 交換,所以各自代表的值也變成了 0 和 1。

# Algorithm

由有限步驟所構成的集合,可以用於解決某一個特定的問題。
其實演算法就是一種解決問題的邏輯思維!

Ex: 假設我們今天要解決的那一個特定問題是「把蘋果做成一杯蘋果汁」

可以透過以下幾個步驟來實現:

  1. 清洗蘋果
  2. 將蘋果削皮、去籽
  3. 將經過步驟 (2) 處理的蘋果放入果汁機
  4. 在果汁機中加入一定比例的水
  5. 按下果汁機啟動按鈕
  6. 將果汁機裡面的蘋果汁倒入玻璃杯中